4

I am trying to allow unknown arguments using argparse, without typing tons of quote marks as in the json.loads solution presented here.

The fire package manages to do this.

For example saving the following program to example.py

import fire

def example_fire_function( **kwargs):
    print(kwargs)

if __name__ == "__main__":
    fire.Fire(example_fire_function)

and invoking it with

python example.py --dringus 4

Outputs {'dringus': 4}, as desired.

Is this possible with just the built in argparse package? I already have a large list of argparse arguments and want to add this on top.

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
Sam Shleifer
  • 1,716
  • 2
  • 18
  • 29
  • No. `parse_known_args` will put the unknowns in a list, but splitting them into a key/value pair is your job. Or just parse `sys.argv` directly. The point to using `argparse` is to give you, the programmer, control over what the user provides (including usage help). There's no need for it if any 'flag value' pair is allowed. – hpaulj Aug 09 '20 at 17:23
  • Does this answer your question? [Is it possible to use argparse to capture an arbitrary set of optional arguments?](https://stackoverflow.com/questions/37367331/is-it-possible-to-use-argparse-to-capture-an-arbitrary-set-of-optional-arguments) – wjandrea Aug 10 '20 at 03:33

1 Answers1

0

No. parse_known_args will put the unknowns in a list, but splitting them into a key/value pair is your job. Or just parse sys.argv directly. The point to using argparse is to give you, the programmer, control over what the user provides (including usage help). There's no need for it if any 'flag value' pair is allowed.

Copied from hpaulj's comment

wjandrea
  • 28,235
  • 9
  • 60
  • 81