docopt / docopts

Shell interpreter for docopt, the command-line interface description language.
Other
504 stars 53 forks source link

How can I have no arguments mean help? #76

Closed kendalldita closed 1 month ago

kendalldita commented 1 month ago

I hope this is the right way to ask questions. If not, please let me know.

How can I have a help message printed if no argument is supplied?

prog ( | -h | --help )

That didn't work, but It could be failing for some other reason.

Sylvain303 commented 1 month ago

Hello @kendalldita,

The parser should be the same as docopt.

I'm not very active as you might have seen.

may be https://stackoverflow.com/questions/tagged/docopt ?

help is the default behavior:

./docopts -h 'usage: prog' :  -h
echo 'usage: prog'
exit 0

Did you look into examples subfolder?

kendalldita commented 1 month ago

Yes. None of them use the absence of any arguments, to mean help.

Aside from that, what would be the syntax to indicate that no argument means help.

Sylvain303 commented 1 month ago

@kendalldita

Oh, you're right. This is a strange habit somewhat a bit rare, on unix at least. Because much command performs task when invoked, even without argument. Teaching people to ask specifically for help -h seems a better approach. So docopts, as docopt, won't help you to do so.

That said, you could use command keywords.

#!/usr/bin/env bash
#
# Launch nuclear missile, use with caution
#
# Usage: launch_missile.sh
#        launch_missile.sh go <where>
#
# Arguments:
#   <where>  coordinate x,y  same as open street map
#
#

# no PATH changes required if docopts binary is in the PATH already
PATH=..:$PATH
source ../docopts.sh
HELP=$(docopt_get_help_string $0)
version='0.1'

parsed=$(docopts -A args -h "$HELP" -V $version : "$@")
#echo "$parsed"                                                                                                                                                                               
eval "$parsed"

display_help()
{
    echo "$HELP"
}

launch_missile()
{
  echo "missile launched to coordinate: $1"
}

if [[ -z ${args[go]} || ${args[go]} != true ]]
then
    display_help
    exit 0
fi

launch_missile ${args[<where>]}
kendalldita commented 1 month ago

Thank you. That makes me wonder if it's clearer to just make no arguments act like -h, without including it in the help text.

kendalldita commented 1 month ago

I think this is resolved. I'm still interested any new ideas about it though.

Sylvain303 commented 1 month ago

In fact, there's a simpler version:

#!/usr/bin/env bash
#
# Launch nuclear missile, use with caution
#
# Usage:  launch_missile.sh go <where>
#
# Arguments:
#   <where>  coordinate x,y  same as open street map
#

# no PATH changes required if docopts binary is in the PATH already
PATH=..:$PATH
source ../docopts.sh
HELP=$(docopt_get_help_string $0)
version='0.1'

parsed=$(docopts -A args -h "$HELP" -V $version : "$@")
#echo "$parsed"
eval "$parsed"

launch_missile()
{
  echo "missile launched to coordinate: $1"
}

launch_missile ${args[<where>]}

removing the no argument usage, give an error displaying the help short message with exit 64

As detailed in the documentation.