docpie icon indicating copy to clipboard operation
docpie copied to clipboard

Unable to use long or short form repeating/list options

Open banhigh opened this issue 5 years ago • 2 comments

Hello,

Thank you for making docpie!

I'm coming from https://github.com/docopt/docopt/issues/448#issuecomment-473663929

I have that exact use case, in that I'm looking for this output:

{
    '--': False,
    '--flag': ['flag1', 'flag2'],
    '-f': ['flag1', 'flag2']
}

Working demo from your link that uses default:

Link

However, how would this actually be called in a real world program?

The only way I can get it to work is use the syntax "-f sflag1 sflag2 sflag3" or "--flag lflag1 lflag2 lflag3" (without "=" that's typically used for the long form)

  1. Using "=" (typically used for the long form) fails parsing: "--flag=flag1 flag2"

    This fails: Link

    Is there a way to get "--flag=flag1 flag2" syntax to work?

  2. What changes will it takes to get it to accept "--flag=flag1 --flag=flag2"?

    This fails: Link

    As you can see, docpie even complains: "--flag/-f requires argument(s)." which shows that the flag is not being parsed correctly :(

  3. Could you also suggest how to update the usage text so that I can call the command like: "--flag=lflag1 -f lflag1 --flag=lflag2 ...  -f sflagn sub_command"?

    The only way I can get this to work is to:

    Link

    which is: "usage: tool [options] [(--flag=<flag>)...] SUB_COMMAND"

    and used as "--flag=flag1 --flag=flag2 --flag=flag3 sub_command"

    output:

    {
        '--': False,
        '--flag': ['flag1', 'flag2', 'flag3'],
        'SUB_COMMAND': 'sub_command'
    }
    

    BUT this unfortunately means that this will not work:

    "--flag=lflag1 -f lflag1 --flag=lflag2 ...  -f sflagn sub_command"

    Link

    even if I tried to change usage to: "usage: tool [options] [(--flag=<flag> | -f <flag>)...] SUB_COMMAND"

    it fails:

    Link

  4. I was expecting the following usage text to work:

    usage: tool [options] SUB_COMMAND
    
    Options: -f (<flag>)... --flag=(<flag>)...  [default: flag1 flag2]
    

    Link

    Error: "DocpieError: `--flag/-f` announced differently ([1, 4, inf], [1, inf])"
    

    ... but if you can suggest a variation of the following so I can get it to work, that would be great too

    Usage: tool [(--flag=<flag>)...|(-f <flag>)...] SUB_COMMAND
    

    Link

banhigh avatar Sep 27 '20 18:09 banhigh

Figured out the solution

... but do you know why this fails with appearedonly=true?

Also, is there a way to have a simpler usage text like:

usage: tool [options] SUB_COMMAND

Options: -f (<flag>)... --flag=(<flag>)...  [default: flag1 flag2]

banhigh avatar Sep 27 '20 18:09 banhigh

Hi, thanks for the detailed description

1. Using "=" (typically used for the long form) fails parsing: "--flag=flag1 flag2"

This looks like a reasonable feature.

Well, unfortunately it's currently not supported. When I write this lib I only considered this usage:

Usage: tool [--flag=<args>...]

which works for --flag f1 f2 but not --flag=f1 f2. The reason is that:

In GNU/POSIX standard, an option(flag) should only accept 0 or 1 arguments. So I can not find a document about the non-standard behavior guild for how I should process this situation.

I need some time to decide if this should be implemented. As it's a bit complex.

2. What changes will it takes to get it to accept "--flag=flag1 --flag=flag2"?

Same as above.

Because

usage: tool [options] [--flag=<flag>...]

options: -f <flag>... --flag=<flag>...  [default: flag1 flag2]

means --flag/-f will accept 1 or more arguments, but with syntax --flag flag1 flag2 or -f flag1 flag2

so --flag=flag1 --flag=flag2 won't work

However, To make it work, the syntax should be

usage: tool [options] [--flag=<flag>]...

options: -f <flag> --flag=<flag>    But the `default` no longer works for this situation

Link

(How should it handle the default? I'm still think...)

3. Could you also suggest how to update the usage text so that I can call the command like: "--flag=lflag1 -f lflag1 --flag=lflag2 ... -f sflagn sub_command"?

Same as above.

usage: tool [options] [--flag=<flag>]... SUB_COMMAND

options: -f <flag> --flag=<flag>    But the `default` no longer works for this situation

Link

4. I was expecting the following usage text to work:

usage: tool [options] SUB_COMMAND
Options: -f (<flag>)... --flag=(<flag>)...  [default: flag1 flag2]

This is because there is a space that is actually not a space.

To check it, copy the text and paste to python as:

print(repr('Options: -f (<flag>)... --flag=(<flag>)...  [default: flag1 flag2]'))

it will print

Options: -f (<flag>)... --flag=(<flag>)... \xa0[default: flag1 flag2]

the \xa0 is not a space but looks like a space

a correct one is: Link (will parse it correctly. But won't match the argv. Because the match is greedy and will eat up SUB_COMMAND's value)

To make it work, we need to prevent --flag's greedy match, as below:

usage: tool [options] [--flag=<flag>]... SUB_COMMAND

Options: -f <flag> --flag=<flag>  default wont work [default: flag1 flag2]

Link

TylerTemp avatar Oct 22 '20 01:10 TylerTemp