google / python-fire

Python Fire is a library for automatically generating command line interfaces (CLIs) from absolutely any Python object.
Other
26.66k stars 1.44k forks source link

How to set the number of args at least one? #475

Closed anemele closed 7 months ago

anemele commented 8 months ago

Hi.

I have a function as follow

def f(*file):
    pass

and use argparse to create cli

parser.add_argument('file', nargs='+', type=str)

It needs at least one argument, given no argument will return

usage: xxx [-h] file [file ...]

I want migrate to fire but fire doesn't perform as expected, it returns

xxx [FILE]..

So, I wonder is there any method to set the number of args?

dbieber commented 7 months ago

To write a Python function that takes at least one arg, have a single required positional arg followed by varargs. E.g.

def f(required, *optional):
  all_args = [required] + optional

While you can't get precisely the behavior you want ("file, [file ...]"), you can get close with:

def f(file, *files):
  files = [file] + files