carlobaldassi / ArgParse.jl

Package for parsing command-line arguments to Julia programs.
Other
235 stars 36 forks source link

`required` arguments are called optional in help text #108

Open adomasbaliuka opened 3 years ago

adomasbaliuka commented 3 years ago

Problem Description

When I declare command line arguments as required, they are required to be passed, as expected. However, the help text (viewed by passing --help as a command line argument) still calls these arguments "optional". More precisely, it lists them after the headline "optional arguments:". Is this intended behavior or a bug?

Code example to reproduce

# Julia Version 1.6.2
# filename "./tmp/argparse_bug.jl" (used below)
using ArgParse

function parse_my_args(args)
    s = ArgParseSettings("Program to look for ArgParse help display bug, which calls required parameters optional.")

    @add_arg_table! s begin
        "--required_argument"
            arg_type = String
            help = "Required string argument."
            required = true
        "--optional_value", "-o"
            help = "Optional integer arument."
            default = 1
            arg_type = Int
    end

    parsed_args = parse_args(args, s)
    println("Parsed args:")
    for (key, val) in parsed_args
        println("  $key  =>  $(repr(val))")
    end

    return parsed_args["required_argument"], parsed_args["optional_value"]
end

function main(args)
    output = parse_my_args(args)
    @show output
end

main(ARGS)

to reproduce:

$ julia ./tmp/argparse_bug.jl --help
usage: argparse_bug.jl --required_argument REQUIRED_ARGUMENT
                       [-o OPTIONAL_VALUE] [-h]

Program to look for ArgParse help display bug, which calls required
parameters optional.

optional arguments:
  --required_argument REQUIRED_ARGUMENT
                        Required string argument.
  -o, --optional_value OPTIONAL_VALUE
                        Optional integer arument. (type: Int64,
                        default: 1)
  -h, --help            show this help message and exit

System information

ArgParse v1.1.4

Julia Version 1.6.2 Commit 1b93d53fc4 (2021-07-14 15:36 UTC) Platform Info: OS: Linux (x86_64-pc-linux-gnu) CPU: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-11.0.1 (ORCJIT, skylake)

carlobaldassi commented 3 years ago

It's not exactly intended behavior. I wouldn't know how to call them though. This is one of the reasons why I disliked this feature. By the way, this is also the same behavior found in Python's argparse:

In [1]: import argparse                                                                                                                     

In [2]: parser = argparse.ArgumentParser()                                                                                                  

In [3]: parser.add_argument('--foo', required = True)                                                                                       
Out[3]: _StoreAction(option_strings=['--foo'], dest='foo', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)

In [4]: parser.parse_args(['-h'])                                                                                                           
usage: ipykernel_launcher.py [-h] --foo FOO

optional arguments:
  -h, --help  show this help message and exit
  --foo FOO

I wouldn't know how to fix this exactly. I guess that it would make sense to change the name "optional arguments" to something else, considered they may not be optional. Maybe "named arguments", in contrast to "positional arguments"? But "optional arguments" is how people refers to them since forever, I think.

Any suggestions?

By the way, right now you can always create a new separate group, e.g.:

julia> s = ArgParseSettings();

julia> add_arg_group!(s, "required arguments", "REQ");

julia> @add_arg_table! s begin
           "--req"
               required = true
               group = "REQ"
           "--opt"
               group = "optional"
       end

julia> parse_args(["-h"], s)
usage: <PROGRAM> --req REQ [--opt OPT] [-h]

optional arguments:
  --opt OPT
  -h, --help  show this help message and exit

required arguments:
  --req REQ
stepanzh commented 2 years ago

Perhaps of the semantic ambiguity, clig.dev define CLI arguments as "args" and "flags" https://clig.dev/#arguments-and-flags.

cpaniaguam commented 1 year ago

Suggestion: Have an option to list user-created argument groups above the "optional" default group? At first I thought they were listed alphabetically but the optional group is always displayed first (at the top).