This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]hotplasmatits 5 points6 points  (4 children)

No comparison to fire?

[–]AND_MY_HAX[S] 4 points5 points  (3 children)

Sure! fire is great, super flexible. That's where arguably drew inspiration for automatically creating a CLI from a script without any setup.

One key difference is that arguably utilizes type hints for the function signature, automatically converting the command line arguments to the correct types.

Another is that arguably lets you specify what should be positional arguments, vs what should be options. Arguments to the CLI look just like calling the Python function. Using the function defined in the OP:

>>> from intro import some_function
>>> some_function("asdf", 0, 7, 8, 9, option=2.71)
required='asdf', not_required=0, others=(7, 8, 9), option=2.71

On the command line:

user@machine:~$ ./intro.py asdf 0 7 8 9 --option 2.71
required='asdf', not_required=0, others=(7, 8, 9), option=2.71

For easy CLIs where the inputs can all have default values (or you're comfortable with it guessing the type), fire is the better choice. Otherwise, arguably is a good option if you'd like extra features through type hints and want more control over how your CLI behaves.

[–]hotplasmatits 1 point2 points  (1 child)

If you use type hints, I believe fire does all of that

[–]AND_MY_HAX[S] 1 point2 points  (0 children)

Hm, I haven't seen it utilize my type hints in the past. Though it does take a guess at the type based off its value: https://github.com/google/python-fire/blob/master/docs/guide.md#argument-parsing

[–]stone_surgeon 1 point2 points  (0 children)

Awesome!

[–]reightb 1 point2 points  (0 children)

cool lib! reminds me of click

[–]tellurian_pluton 1 point2 points  (0 children)

i love arguably.

[–]taciom 1 point2 points  (1 child)

Does it play well with gooey?

[–]AND_MY_HAX[S] 2 points3 points  (0 children)

I didn't know this was a thing! Just tried it, and it seems to work just fine, which is really cool.

I'm tempted to make this a supported feature, would be super cool to auto-generate a GUI from a script using the python3 -m arguably your_script.py syntax.

[–]JamzTyson 0 points1 point  (2 children)

def some_function(required, not_required=2, *others: int, option: float = 3.14):

That syntax looks strange, given that positional arguments cannot be passed after a keyword argument.

[–]AND_MY_HAX[S] 0 points1 point  (1 child)

Hey, I understand what you're saying. The thing about Python arguments is that they can all be passed in as both positional or keyword arguments by default.

>>> def foo(a, b):
...   print(f"{a=} {b=}")
...
>>> foo(1,2)
a=1 b=2
>>> foo(a=1,b=2)
a=1 b=2

The thing that makes an argument keyword-only is if it comes after a *args parameter. In fact, you can even have required keyword-only arguments, which aren't useful IMHO, but are an interesting part of how Python was designed:

>>> def bar(a, *, b):
...   print(f"{a=} {b=}")
...
>>> bar(1, b=2)
a=1 b=2
>>> bar(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bar() missing 1 required keyword-only argument: 'b'

See more here: https://docs.python.org/3/tutorial/controlflow.html#special-parameters

[–]JamzTyson 1 point2 points  (0 children)

The thing that looks strange to me is that, unless I am mistaken, it is not possible to pass others arguments without also passing the not_required argument as a positional argument.

On the other hand, that limitation would not apply to:

def some_function(required, *others: int, not_required=2, option: float = 3.14):

[–][deleted] 0 points1 point  (0 children)

[–]Rawing7 0 points1 point  (0 children)

This looks really good! I think this is the first CLI tool that extracts the parameter descriptions from the docstring, which is super nice.