E
E
Egorian2018-06-02 16:42:09
Python
Egorian, 2018-06-02 16:42:09

How to call a function with arguments from the console?

How to call a function with arguments from the console?
Let's say we have a function like this:

def hello_somebody(somebody):
               print("hello %s" % somebody)

How to call this function from cmd windows?
I know it can be done like this:
def hello_somebody(somebody):
    print("hello %s" % somebody)
hello_somebody(input())

and call the file.
I want the function call to be like this: python file.py hello_somebody("Igor")
Is it possible to implement this or would the option above be easier?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maelstorm, 2018-06-07
@Egorian

You can also look towards a ready-made library, 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 command line:
python calculator.py double 10  # 20
python calculator.py double --number=15  # 30

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question