Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define optional argument with exactly 1 value, + default value #339

Open
nmoreaud opened this issue Mar 20, 2024 · 7 comments
Open

Define optional argument with exactly 1 value, + default value #339

nmoreaud opened this issue Mar 20, 2024 · 7 comments

Comments

@nmoreaud
Copy link

nmoreaud commented Mar 20, 2024

Hello,

I try to define an optional parameter with a value + default value, but it doesn't work as I expect.
I use version 3.0.

What I'd like to obtain:

  • Optional [] in usage section: Usage: [--format1 VAR] --format2 VAR --format3 VAR [--format4 VAR]
    ok for format1 and format4
  • Exception if argument is called without value: ok for --format2 and --format3
  • No mention of [nargs=0..1] in argument section:
--format1                  Image format [jpg|png] [nargs=0..1] [default: "jpg"]
--format2                  Image format r1 [jpg|png] [default: "jpg"]                            => ok
--format3                  Image format r [jpg|png] [nargs=0..1] [default: "jpg"]
--format4                  Image format 1 [jpg|png] [default: "jpg"]                             => ok
  • No mention of [required] in argument section: ok for all
  • Listed in "Optional arguments" section: ok for all

Could you help me?

Here is a sample program:

        command.add_argument("--format1")
            .default_value(string("jpg"))
            .help("Image format [jpg|png]");

        command.add_argument("--format2")
            .default_value(string("jpg"))
            .required()
            .nargs(1)
            .help("Image format r1 [jpg|png]");

        command.add_argument("--format3")
            .default_value(string("jpg"))
            .required()
            .help("Image format r [jpg|png]");

        command.add_argument("--format4")
            .default_value(string("jpg"))
            .nargs(1)
            .help("Image format 1 [jpg|png]");
@Eng-MohamedHussien
Copy link
Contributor

  • I suggest reading README, it has answers of all your questions.

@Eng-MohamedHussien
Copy link
Contributor

Eng-MohamedHussien commented Aug 27, 2024

Let's discuss sample program:

command.add_argument("--format1")
            .default_value(string("jpg"))
            .help("Image format [jpg|png]");
  • If you didn't provide that argument, no exception will be fired as you provided a default value.
    Screenshot from 2024-08-27 20-27-29
  • --help output:
--format1      Image format [jpg|png] [nargs=0..1] [default: "jpg"]
  • You can do the same for argument --format4.

@Eng-MohamedHussien
Copy link
Contributor

  • --format2 & --format3 are same.
command.add_argument("--format3")
            .default_value(string("jpg"))
            .required()
            .help("Image format r [jpg|png]");
  • By adding here default_value you lost the exception that will be fired when no argument is passed (Read previous mentioned section).

@Eng-MohamedHussien
Copy link
Contributor

Eng-MohamedHussien commented Aug 27, 2024

  • Correct version:
command.add_argument("--format1")
    .default_value(string("jpg"))
    .help("Image format [jpg|png]");

command.add_argument("--format2")
    .required()
    .help("Image format [jpg|png]");

command.add_argument("--format3")
    .required()
    .help("Image format [jpg|png]");

command.add_argument("--format4")
    .default_value(string("jpg"))
    .help("Image format [jpg|png]");
  • Help output:
    Screenshot from 2024-08-27 20-42-07

@Eng-MohamedHussien
Copy link
Contributor

@nmoreaud,
If you don't have extra questions, kindly close the ticket.

@Eng-MohamedHussien
Copy link
Contributor

@p-ranav,
Kindly close the ticket.

@nmoreaud
Copy link
Author

nmoreaud commented Sep 3, 2024

Hello
I cannot test now, but I don't really see an answer to my question.
Basically you recommend to use either "default" or "required" and none of these two syntaxes seem to address every point of the question.
The 4 syntaxes in the ticket description are 4 attempts to address every point of the question, and none of them is correct.
But you can also close the ticket as "by design".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants