docopt / docopt.cpp

C++11 port of docopt
Boost Software License 1.0
1.04k stars 146 forks source link

Unable to add short version of option #132

Open AntonioCS opened 4 years ago

AntonioCS commented 4 years ago

I have the following:

static const char static const char USAGE[] =[] =
R"(Usage: my_program [-ho <file>] [--output=<file>] [--remove_comments] [--no_separators] [--no_timestamp] [--silent] <path>

    -h --help                   show this
    -o <file> --output=<file>   specify output file [default: ./default.h]
    --remove_comments           remove one line comments from the header files
    --no_separators             don't add // <filename> START|END separators
    --no_timestamp              don't add timestamp
    --silent                    no info text
)";

I then call my_program: my_program.exe --output=test ./dir/ this gives me an error of Arguments did not match expected patterns

If I remove the -o <file> so that USAGE becomes:

static const char USAGE[] =
R"(Usage: my_program [-h] [--output=<file>] [--remove_comments] [--no_separators] [--no_timestamp] [--silent] <path>

    -h --help                   show this
    --output=<file>             specify output file [default: ./allinone.h]
    --remove_comments           remove one line comments from the header files
    --no_separators             don't add // <filename> START|END separators
    --no_timestamp              don't add timestamp
    --silent                    no info text
)";

All works as expected. Shouldn't the -o <file> work?

jaredgrubb commented 4 years ago

I see this which looks wrong [-ho <file>] ... is that causing the problem?

AntonioCS commented 4 years ago

I don't know. The error message is just the generic: params are not right.. What is wrong exactly? I want to be able to pass a file either by doing -o or --output=

AntonioCS commented 4 years ago

I also want it to be mandatory that if you pass -o or --output that you pass

AntonioCS commented 4 years ago

To note if I do this:

my_program -o file path

I get the following error:

Unexpected argument: -o, file, path

I also get an error with --output=file

I have tried:

- -o <file> --output=<file>       specify output file [default: ./default.h]
- -o file --output=file       specify output file [default: ./default.h]
- -o --output=file       specify output file [default: ./default.h]

Is what I want impossible?

indianakernick commented 4 years ago

What you’re trying to do is not impossible. You just haven’t got the syntax quite right. Try this:

Usage:
  my_program [--output=<file>]

Options:
  -o, --output <file>  specify output file [default: ./default.h]

I encourage you to play around with this online tool. It’s great for prototyping.

AntonioCS commented 4 years ago

@Kerndog73 So following your suggestion I have changed it to:

static const char USAGE[] =
R"(Usage: my_program [-h] [--output=<file>] [--remove_comments] [--no_separators] [--no_timestamp] [--silent] [path]

Arguments:
    [path]                      path to directory of header files

Options:
    -h --help                   show this
    -o, --output <file>         specify output file [default: ./default.h]
    --remove_comments           remove one line comments from the header files
    --no_separators             don't add // <filename> START|END separators
    --no_timestamp              don't add timestamp
    --silent                    no info text
)";

Added Arguments and Options section. I calling the program like:

my_program --output=file path

Still getting the same error:

Unexpected argument: --ouput=file, path
Usage: onlyoneheader [-h] [--output=<file>] [--remove_comments] [--no_separators] .........
indianakernick commented 4 years ago

That’s because file path is two separate arguments. You need to use a backslash. file\ path.

AntonioCS commented 4 years ago

Not sure if I follow you.. but I did

my_program --ouput=file\ path

and got:

Unexpected argument: --ouput=file\, path

Not exactly sure why this is breaking now... I mean.. this at least worked before with just --output=file path (when I removed the -o stuff) and now it's breaking... Is there a way to get some better error messages? Like -vvv for errors?

jaredgrubb commented 4 years ago

You spelled it ouput in the example you typed (missing a 't'). Does that fix it?

No, the errors are not helpful and would be super helpful (but super hard :( ) .. there is an Issue open fort hat.

AntonioCS commented 4 years ago

@jaredgrubb this is what I get for trying to program late at night! That was indeed the issue. So to provide full context here is what I have currently:

static const char USAGE[] =
R"(Usage: my_program [-h] [--output=<file>] [--remove_comments] [--no_separators] [--no_timestamp] [--silent] <path>

Arguments:
    <path>                      path to directory of header files

Options:
    -h --help                   show this
    -o --output=<file>          specify output file [default: ./default.h]
    --remove_comments           remove one line comments from the header files
    --no_separators             don't add // <filename> START|END separators
    --no_timestamp              don't add timestamp
    --silent                    no info text
)";

Now the -o is not longer a boolean option and everything works. One thing I would like to know is if it's possible to add back the -o to the Usage part or does it only have to show up in the Options like I guess the --help does? Possibly also add a few more examples that have this exact scenario? Found some examples that were similar but don't think they were exactly like what I had.

Thanks @jaredgrubb , @Kerndog73. for helping me with this, was really going crazy.