On Jul 30, 2019, at 15:21, [email protected] wrote:
>
> Maybe the def __main__() argument is already a dead horse, given the number
> of discussions it has created that have ended nowhere, but I think one
> argument in favour of its implementation would be including argument parsing
> in it, for example:
>
> # main.py
> def __run__(first_num, second_num, print_operation=False):
Couldn’t you get nearly the same effect in Python today just by adding one line:
__run__(*sys.argv[1:])
That doesn’t do any fancy parsing of the arguments, but, except for the -h
magic, neither does your proposal.
Also:
> $ python main.py -h
Does —help also work? (It’s pretty weird that you use a long arg for verbose
but a short arg for help.)
> $ python main.py 1 2 --verbose
> 1 + 2 = 3
But if I run the same thing in the more traditional way:
$ python main.py —verbose 1 2
… I’ll get an error, because the first argument isn’t an int and can’t be added
to 1.
Also:
$ python main.py 1 2 —quiet
… does the same thing as —verbose, which is pretty misleading.
Not that I haven’t done the quick hacky “this script isn’t going to be used by
anyone but me, and only for the next two days, and it only needs one flag, so
just assume any third arg is that flag” thing. But that’s hardly something you
want a language feature to actively encourage.
There are lots of great third-party libraries that make turning a function into
a command-line utility a lot easier than using argparse. I think whenever you
want anything more than argv and don’t want argparse, you should probably just
use one of those libraries.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/IE7OLODPNLBMCZO3DBACOF3GCGLAD3V7/
Code of Conduct: http://python.org/psf/codeofconduct/