0

I have a Python script with a few functions.

Is it possible to launch the script from terminal and then leap straight to a particular function, following conditions within the script? I.e. python3 script.py function.

I was thinking this would be a good alternative to asking the user to answer Y/N to starting functions within script.

Steven Diffey
  • 73
  • 1
  • 7
  • 2
    python (similar to other languages) has `sys.args` to get arguments from command line as list - and `sys.argv[1]` should have your `function` as string and you would have to use `if/else` to run correct function. And if you want to run script with more complex paramaters - like `--func your_function` then you can use standard module [argparse](https://docs.python.org/3/library/argparse.html). There are also external modules for this - ie. [click](https://click.palletsprojects.com/en/8.1.x/). You may also find some posts on blogs which compare other modules – furas Aug 06 '22 at 21:55
  • @furas *`sys.argv` – wjandrea Aug 06 '22 at 22:01
  • 2
    @wjandrea you are right. Funny is that first I wrote `sys.args` and few words later I wrote correctly `sys.argv[1]` :) – furas Aug 06 '22 at 22:03
  • 2
    @Steven I just found another question that's even closer to what you're asking and added it as another duplicate :) Did you get a notification for that btw? I've never thought about it before. – wjandrea Aug 06 '22 at 22:10

0 Answers0