On Tue, Oct 20, 2020 at 10:19 PM Guido van Rossum <gu...@python.org> wrote:
>
> I think it would be a tad more convincing if there was a way to pass 
> arguments too (even if just a list of strings). At the very least extra 
> arguments should end up in sys.argv[1:].

Could python -m 'module:thunk' have exactly the same behavior with
respect to arguments as `python3.8 -m module` does today?

```
$ cat bar.py
import pprint, sys

def thunk():
    pprint.pprint(sys.argv)

if __name__ == "__main__":
    thunk()

$ python3.8 -m bar -- -1 --two --three=3
['/Users/michael/bar.py', '--', '-1', '--two', '--three=3']
```

So then with the same bar.py, `python -m bar:thunk -- -2 --three
--four=4` would print `['/Users/michael/bar.py', '--', '-1', '--two',
'--three=3']`. I like this better than my previous suggestion to
shorthand python -c.

> Then again, presumably the function must be specially crafted for this usage. 
> Why can't you just specially craft a module's main()?

I'm not sure I know what you mean by "specially crafted", other than
the function only needs not require any formal parameters. It doesn't
need to be special-er than that. It can handle args via sys.argv, as
you suggested. Most of the `main` functions I write today are just
like that.

> On Tue, Oct 20, 2020 at 7:10 PM Michael Smith <mich...@smith-li.com> wrote:
>>
>> There are many ways to invoke a function from the commandline. You can
>> use setuptools' console_scripts entrypoint. You can use a decorator
>> from click. And of course you can always do the classic
>>
>> if __name__ == "__main__":
>>   main()
>>
>> to call whatever main() is. But there are inconveniences with these
>> approaches. For setuptools, you have to actually run setup, somehow.
>> For click, you have to install click. And for __name__, you are either
>> locked into a single function name, or you have to write some arg
>> parsing to determine what name to use.
>>
>> I propose it would be nice to be able to call a function from python,
>> using syntax like
>>
>> python -m module:thunk
>>
>> The simplest proposal, I think, is if the function must accept no
>> arguments -- or at least no required ones. This could be as
>> straightforward as just being shorthand for
>>
>> python -c 'import module; module.thunk()'
>>
>> and remove a small amount of code that is repeated very frequently.
>>
>> I picked the colon syntax because that is what several other tools
>> that enable calling functions from the commandline seem to do, but if
>> your only objection is the specific syntax I picked, please propose a
>> different one.
>>
>> What do you think?
>> _______________________________________________
>> Python-ideas mailing list -- python-ideas@python.org
>> To unsubscribe send an email to python-ideas-le...@python.org
>> https://mail.python.org/mailman3/lists/python-ideas.python.org/
>> Message archived at 
>> https://mail.python.org/archives/list/python-ideas@python.org/message/JWLFOEOVGQKPD6F7ZHS5PWYIEMYOFUQ5/
>> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
>
> --
> --Guido van Rossum (python.org/~guido)
> Pronouns: he/him (why is my pronoun here?)
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/RTQI773MRNQA6SEEGXBUB6CHBNU66ELA/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to