Python - Creating a Command Line Argument (argparse)
$ cd -h
$ sudo apt-get -h
$ dpkg --help
$ arp -a
With python we can create programs to run on the terminal. Example :
import argparse #module
def server_start (server, port):
print "Starting% s ... OK"% server
print "% s port:% s"% (server, port)
def db_start (db, port):
print "Starting% s ... OK"% db
print "% s port:% s"% (db, port)
def main ():
#create Instance parser
parser = argparse.ArgumentParser (description = "Program start Server")
parser.add_argument (
"--server",
type = str,
required = True,
help = "Your Server (apache, nginx etc)"
)
parser.add_argument (
"--server_port",
type = int,
default = 80,
help = "Your Server port"
)
parser.add_argument (
"- db",
type = str,
required = True,
help = "Your Database (mysql, oracle etc)"
)
parser.add_argument (
"--db_port",
type = int,
default = 3060,
help = "Your Database port"
)
# make the parser
args = parser.parse_args ()
#call function
server_start (args.server, args.server_port)
db_start (args.db, args.db_port)
if __name__ == '__main__':
play ()
when we create a program / parser code, then by default we can use the -h command or --help to display help
$ python parser.py -h
or
$ python parser.py --help
then the output
Server start program
optional arguments:
-h, --help show this help message and exit
- server SERVER Your Server (apache, nginx etc)
--db DB Your Database (mysql, oracle etc)
note that according to the coding code, our Command Line program
requires two arguments, namely - server and - db
If we run the program without any argument
$ python parser.py
then the message will appear as follows
usage: parser.py [-h] --server SERVER --db DB
parser.py: error: --server argument is required
With the code we make as above we require the User to
use the Command line as follows:
$ python parser.py --server = apache --server_port 81 --db mysql --db_port = 3030
or (without using = operator)
$ python parser.py - server apache --server_port 81 --db mysql --db_port 3030
Command Line Argument Details
In general, to make our arguments go in the following ways:
1. Positional Command line argument
p.add_argument ("input_dir", ...)
p.add_argument ("input_dir", "output_dir" ...)
This means that the input_dir argument must be in the first position
and then output_dir must be in the second position. so we have to
run terminal like this:
# $ python example.py {path / to / input} {path / to / output}
$ python example.py / home / image / original / home / image / copy
2. Optional command line argument
Because of Optional, we can place our arguments anywhere
during execution. Therefore Optional Arguments must be added
"-" or "-" sign
Example:
$ python parser.py --db = mysql --db_port = 3021 --server = apache --server_port = 81
note that the position is free (unlike the previous example)
In the example above we also see all the arguments using the "-" mark that actually means the Command length. we can short it by adding an argument marked "-"
parser.add_argument (
"-s",
"--server",
type = str,
required = True,
help = "Your Server (apache, nginx etc)"
)
- server can we rename or shorten with -s so the execution can be like this
python parser.py -s = apache --server_port 81 --db mysql --db_port = 3030
It's the same as shortened --help with -h
Parser Attribute
parser.add_argument (
"--server",
type = str,
required = True,
help = "Your Server (apache, nginx etc)"
)
in the example above type, required, help is an Attribute. Here are the main attributes
which is often used in .add_argument ()
help
help is a String that we can use for additional information on an Argument.
highly recommended to add this attribute to make it easier for users to use
Command that we make. Can be accessed by default with the -h or --help command
required
If we feel an argument is mandatory, it can use this attribute.
required = True
if we are set required, then we only use type 2 taitu Optional command line argument
because the Positional Argument is certainly compulsory.
type
type is the data type of the Argument value. the most common are: int, float, str.
type = str
action
Explain what will happen if a particular argument is executed. usually will
save certain values. Yes
0 Comments