Console/GUI scripts with command-line arguments #3352
-
I'm not sure if this is the right place to ask this question, but what is the recommended approach to make console/GUI scripts accept command-line arguments? The current Setuptools docs on Entry Points don't mention this, but the Python packaging user guide says that the functions corresponding to console/GUI scripts will be invoked with no arguments. To test this, I tried configuring a console script for the following function: def hello(foo):
print('Hello,', foo) and indeed on trying to invoke it like so:
the argument Then what is the correct way to pass command-line arguments? I checked the Pip source code to see how they do it and it seems that they manipulate def main(args: Optional[List[str]] = None) -> int:
if args is None:
args = sys.argv[1:]
... So is manipulating |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @codeandfire, your code should have access to The command practice however is to use a library to construct an interface for this. If you want to stick with the standard library, you can try It is very common to see people also using |
Beta Was this translation helpful? Give feedback.
Hi @codeandfire, your code should have access to
sys.argv
, that is the standard way of passing command line arguments.The command practice however is to use a library to construct an interface for this.
If you want to stick with the standard library, you can try
argparse
.There is a tutorial here and a minimal example here.
It is very common to see people also using
click
andtyper
instead ofargparse
.