docopt / docopts

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

--debug flag not working as intended? #48

Closed impguard closed 3 years ago

impguard commented 3 years ago

Seeing debug print strange output on OSX:

#!/usr/bin/env bash

eval "$(./docopts-osx --debug -h - : "$@" <<EOF
A script

Helpful commands to work with this project.

Usage:
  ./make admin COMMAND
EOF
)"

echo "$COMMAND"

Output I see

$ ./test.sh admin hi
./test.sh: line 13: --debug: command not found
./test.sh: line 14: --help: command not found
./test.sh: line 15: --no-declare: command not found
./test.sh: line 16: --no-help: command not found
./test.sh: line 17: --no-mangle: command not found
./test.sh: line 18: --options-first: command not found
./test.sh: line 19: --separator: command not found
./test.sh: eval: line 20: syntax error near unexpected token `newline'
./test.sh: eval: line 20: `           --version : <nil>'

I wanted to test using [--] since it wasn't working as I expected. But the debug output is not presenting something useful. If I remove the --debug flag I get:

$ ./test.sh admin hi
hi
Sylvain303 commented 3 years ago

Hi @impguard,

The --debug mode is for developer, when you want debug the command docopts itself. I think it is not documented. What to did you expect exactly?

It works, as I would expect:

I put your code into a variable:

$ HELP="A script
> 
> Helpful commands to work with this project.
> 
> Usage:
>   ./make admin COMMAND
> "

$ echo "$HELP"
A script

Helpful commands to work with this project.

Usage:
  ./make admin COMMAND

Using the debug mode, you get an output not suitable for eval: I will describe the output bellow.

$ ./docopts -h "$HELP" --debug : 
################## golang ##################
             --debug : true
              --help : A script

Helpful commands to work with this project.

Usage:
  ./make admin COMMAND

        --no-declare : false
           --no-help : false
         --no-mangle : false
     --options-first : false
         --separator : ----
           --version : <nil>
                  -A : <nil>
                  -G : <nil>
                   : : true
              <argv> : []
                 doc : A script

Helpful commands to work with this project.

Usage:
  ./make admin COMMAND
        bash_version : 
echo 'error: 
Usage:
  ./make admin COMMAND' >&2
exit 64

Here I didn't send any param to the bash parser: the colon : separator is mandatory and followed by nothing. We got only one section, the golang section, showing all value of all switches for the binary.

If I add some parameter for the bash script:

./docopts -h "$HELP" --debug : admin "reboot"
################## golang ##################
             --debug : true
              --help : A script

Helpful commands to work with this project.

Usage:
  ./make admin COMMAND

        --no-declare : false
           --no-help : false
         --no-mangle : false
     --options-first : false
         --separator : ----
           --version : <nil>
                  -A : <nil>
                  -G : <nil>
                   : : true
              <argv> : [admin reboot]
                 doc : A script

Helpful commands to work with this project.

Usage:
  ./make admin COMMAND
        bash_version : 
################## bash ##################
             COMMAND : reboot
               admin : true
----------------------------------------
COMMAND='reboot'
admin=true

We got a new section ################## bash ################## Which shows what the parser extracted and mapped the bash's argument to the HELP string given.

And finally the the output for eval:

COMMAND='reboot'
admin=true

Could you precise your question, regarding the information above?

impguard commented 3 years ago

Ah, I did not consider that the debug output was outputting to standard out and would be eval'd.

That will resolve this issue. However, it does indicate that [--] does not work as intended currently. But I can create a separate issue for that.