-3

How to handle multiple arguments in python for example. I want to use -i for input, -o for output, -v for version(optional). First of all how will I define function for that.

Example format:

Usage: %s -i <inputFileName> -o <outputFileName> [-f <version>]
Lescurel
  • 10,749
  • 16
  • 39
psy
  • 1
  • 2
  • 1
    Welcome to SO! Please take the [tour] and read [ask]. In the future, please do some research of your own before asking a question. For example if you google "python command line arguments", you can easily find [this question](https://stackoverflow.com/questions/1009860/how-to-read-process-command-line-arguments) I'm recommending as a duplicate. – wjandrea Dec 07 '20 at 14:50
  • @wjandrea thanks for welcoming. The thing is sometimes a beginner like me have trouble to find exact answer & website in the internet. And also thanks for the website reference. – psy Dec 07 '20 at 16:00

1 Answers1

1

I would recommend you read into the argparse standard library. It should have more than you need to start parsing command line arguments!

from argparse import ArgumentParser

parser = ArgumentParser()
parser.add_argument('-i', '--input', required=True, type=str, help="...")
...
args = parser.parse_args()

print(args.input)
wakey
  • 2,283
  • 4
  • 31
  • 58