Creating command line keys in python

For example, sqlmap uses python sqlmap.py -h.

The above command lists all the available keys in sqlmap, and -hthis is the switch itself.

When you create a python tool for use in a terminal, what is the main way to create a switch?

A welcoming world example would be greatly appreciated!

+5
source share
2 answers

These are command line options. You can use the stdlib module argparsefor this.

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                   help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                   const=sum, default=max,
                   help='sum the integers (default: find the max)')

args = parser.parse_args()
print args.accumulate(args.integers)
+8
source

Arguments of the command line with which the program was launched are displayed as a list of lines in sys.argv, moreover, sys.argv[0]the name of the program.

, , , getopt optparse. argparse, .

0

All Articles