How to call a function with arguments from the console?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
4 views
How to call a function with arguments from the console?
We have, for example, the function:
def hello_somebody(somebody): print("hello %s" % somebody)

As this function is called from the windows cmd?
Know that you can do this:
def hello_somebody(somebody): print("hello %s" % somebody) hello_somebody(input())

and call file.
I want a function like this one:python file.py hello_somebody("Igor")
If you can implement it or the easier option above?
by | 4 views

2 Answers

0 like 0 dislike
You can also look in the direction of ready-made libraries, for example python-fire
An example from the documentation:
import fire class Calculator(object): """A simple calculator class.""" def double(self, number): return 2 * number if __name__ == '__main__': fire.Fire(Calculator)

Call from the command line:
python calculator.py double 10 # 20 python calculator.py double --number=15 # 30
by
0 like 0 dislike
In the simplest cases, use sys.argv, in more complex https://docs.python.org/3/library/argparse.html and such
by
110,608 questions
257,186 answers
0 comments
28,082 users