run python with options 1

run python with options

import argparse
import sys

parser = argparse.ArgumentParser(description="Does some awesome things.")
parser.add_argument('message', type=str, help="pass a message into the script")

if __name__ == '__main__':
    args = parser.parse_args(sys.argv[1:])
    print args.message

Here is what the above code is Doing:
1. We create a parser object.
2. We add an argument to the parser object.
3. We parse the arguments.
4. We print the message.

The parser object is an instance of the ArgumentParser class.
The ArgumentParser class has a method called add_argument.
The add_argument method takes a string as an argument.
The string is the name of the argument.
The add_argument method also takes a type argument.
The type argument is the type of the argument.
The add_argument method also takes a help argument.
The help argument is a string that describes the argument.

The ArgumentParser class has a method called parse_args.
The parse_args method takes a list of strings as an argument.
The parse_args method returns an object.
The object has an attribute for each argument.
The attribute name is the name of the argument.
The attribute value is the value of the argument.

Similar Posts