0

I'm trying to use argparse module to parse command-line arguments, and I would like to use *args as the number of arguments is not fixed.

My code:

if __name__ == '__main__':
    
    parser = argparse.ArgumentParser()
    parser.add_argument("program", help='Name of the program')
    parser.add_argument("type", help='Type of program')
    parser.add_argument("date", help='Date of the file')

These 3 arguments are a must: program, type and date. However, the next arguments are optional (sometime required, sometime not). So, I thought of using *args for the other arguments, but I'm unsure how that's done using argsparse.

The optional arguments would look like:

if __name__ == '__main__':
    
    parser = argparse.ArgumentParser()
    parser.add_argument("program", help='Name of the program')
    parser.add_argument("type", help='Type of program')
    parser.add_argument("date", help='Date of the file')

    #below arguments are optinal. Hence, I may need to pass all of them in one scenario, or just 1-2 in 
    another scenario.

    parser.add_argument("option1", help='optinal 1')
    parser.add_argument("option2", help='optinal 2')
    parser.add_argument("option3", help='optinal 3')
    parser.add_argument("option4", help='optinal 4')

Please help. Thanks in advance.

PKV
  • 167
  • 3
  • 13
  • 2
    This is hard to answer in the general sense, can you give more context? Have you looked at e.g. [`nargs`](https://docs.python.org/3/library/argparse.html#nargs)? – jonrsharpe Jul 15 '20 at 16:37
  • 1
    _nod_; generally, one should either use `nargs='+'`, or a subparser for the (sub)commands that may require those optional arguments, or add your own validation logic that calls the parser's shared print-an-error logic when it fails. We really need a question that goes into more details to know what the right answer is here. – Charles Duffy Jul 15 '20 at 16:38
  • Does this answer your question? [Argparse optional positional arguments?](https://stackoverflow.com/questions/4480075/argparse-optional-positional-arguments) – Luke Storry Jul 15 '20 at 16:38
  • 1
    Have you read any/much of the argparse documentation? Notice any thing about `positional` arguments, and `optionals`? `optionals/flagged` arguments are defined with names like `--option1`, and are by default "optional". – hpaulj Jul 15 '20 at 19:13

2 Answers2

1

https://docs.python.org/3/library/argparse.html#the-add-argument-method

ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])

The name of flags is a *args argument; you can specify a single name for positional or multiple for optional (e.g. `('-f','--foo', '--foobar',...)

The other arguments are received as **kwargs, so are usually provided as you do with the help argument.

Since there are lots of possible parameters, I'd suggest starting with the simplest, and experiment.

Most important is the https://docs.python.org/3/library/argparse.html#name-or-flags. And secondly the https://docs.python.org/3/library/argparse.html#nargs.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • I'm pretty sure OP's asking about command-line arguments, not function arguments. – wjandrea Jul 15 '20 at 22:35
  • @wjandrea, true. But `*args` is only applicable to function arguments. But if the OP wants to arguments that are optional he has to use one or more of the document parameters. – hpaulj Jul 16 '20 at 03:11
-1

Use keyword required=bool

parser = argparse.ArgumentParser()
parser.add_argument("-p","--program", help='Name of the program', required=True)
parser.add_argument("-f", "--foo", help='Foo', required=False)
unil
  • 194
  • 1
  • 8
  • 1
    I think a beginner should avoid the `required` parameter. Flagged arguments such as yours are called `optionals` for a reason. The simple split between `positionals` for required values, and flagged ones for optionals is least confusing. Choosing a good `default` is more useful than setting `required=True`. – hpaulj Jul 15 '20 at 20:54
  • Using options is a good idea, but setting `required` is not. For options like `--foo`, `required=False` is the default. Meanwhile, there's no good reason to make `program` an option instead of a positional argument. – wjandrea Jul 15 '20 at 20:59