p-ranav / argparse

Argument Parser for Modern C++
MIT License
2.59k stars 244 forks source link

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

Open nmoreaud opened 5 months ago

nmoreaud commented 5 months ago

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:

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 commented 1 week ago
Eng-MohamedHussien commented 1 week ago

Let's discuss sample program:

command.add_argument("--format1")
            .default_value(string("jpg"))
            .help("Image format [jpg|png]");
Eng-MohamedHussien commented 1 week ago
Eng-MohamedHussien commented 1 week ago

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](https://github.com/user-attachments/assets/2c066aed-fb6e-4105-9565-9c5cb3130500)
Eng-MohamedHussien commented 1 week ago

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

Eng-MohamedHussien commented 4 days ago

@p-ranav, Kindly close the ticket.

nmoreaud commented 4 days ago

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".