简体   繁体   中英

Argparse: optional subparsers with store_true flags

Forgive me if my terminology is off, but I'm looking for a way to add a subparser to an optional argparse argument, with store_true flags on each of the args.

Ideally, I'd like to use the following syntax to reference the boolean value of --html in the shodan_parser subparser:

if args.shodan.html:
    print("Doing a thing") 

parser = argparse.ArgumentParser()
parser.add_argument("-r", "--reverse-dns", help="rDNS on host", action="store_true")
parser.add_argument("-s", "--shodan", help="perform SHODAN query on discovered IPs", action="store_true")
parser.add_argument("targets", help="IPv4 addresses to search for", nargs="+")

subparsers = parser.add_subparsers()
shodan_parser = subparsers.add_parser("shodan", help="SHODAN options")
shodan_parser.add_argument("--html", action="store_true")
shodan_parser.set_defaults(which='shodan')

Output:

(venv)[nott@admin gumdrop]$ python gumdrop.py google.ca --shodan --html
usage: gumdrop.py [-h] [-r] [-e] [-s] targets [targets ...] {shodan} ...
gumdrop.py: error: too few arguments

(venv)[nott@admin gumdrop]$ python gumdrop.py --shodan --html google.ca askjeeves.ca
usage: gumdrop.py [-h] [-r] [-e] [-s] targets [targets ...] {shodan} ...
gumdrop.py: error: invalid choice: 'askjeeves.ca' (choose from 'shodan')

Any suggestions?

You'll need to change the switch from "-h" to something else (or disable the help), because the "-h" switch is already used by the help menu

parser = argparse.ArgumentParser()
parser.add_argument("-r", "--reverse-dns", help="rDNS on host", action="store_true")
parser.add_argument("-s", "--shodan", help="perform SHODAN query on discovered IPs", action="store_true")parser.add_argument("targets", help="IPv4 addresses to search for", nargs="+")

subparsers = parser.add_subparsers()
shodan_parser = subparsers.add_parser("shodan", help="SHODAN options")
shodan_parser.add_argument("--html", action="store_true")
shodan_parser.set_defaults(which='shodan')

args = parser.parse_args()

if args.html:
    print("Doing a thing")
parser = argparse.ArgumentParser()
parser.add_argument("-r", "--reverse-dns", help="rDNS on host", action="store_true")
parser.add_argument("-s", "--shodan", help="perform SHODAN query on discovered IPs", action="store_true")
parser.add_argument("targets", help="IPv4 addresses to search for", nargs="+")

Does this --shodan optional (flag) have anything to do with the subparser name? What is its purpose? Are you confusing a flag with a subparser?

The subparser is also a positional. Using both a positional with nargs='+' and a subparser might work, but is likely to cause confusion. When does the list of targets end, and the subparser (and its arguments) begin?

subparsers = parser.add_subparsers()
shodan_parser = subparsers.add_parser("shodan", help="SHODAN options")
...

your output

(venv)[nott@admin gumdrop]$ python gumdrop.py google.ca --shodan --html
....

I'm guessing that this sets targets=['google.ca'] , shodan=True . But you haven't given it a subparser command. The --html is an unknown.

(venv)[nott@admin gumdrop]$ python gumdrop.py --shodan --html google.ca askjeeves.ca
...

Now you set shodan=True (the main parser flag). Again --html is unknown. It sets target=['google.ca'] . But now it tries to interpret askjeeves.ca as a subparser command . But it does not match the available choices.

I think these lines would work:

python gumdrop.py google.ca askjeeves.ca shodan --html
python gumdrop.py --shodan google.ca shodan --html

I'd suggest dropping the whole subparser bit. It's just confusing things, for you and your users. Go ahead and include --html in the main parser. It's optional, so it can be used, or ignored, at will. It you must use subparsers, review the documentation, and try some simpler examples.

As for getting args.shodan.html , that's a much more advanced issue, involving the nesting of namespaces. For now be happy if you get args.html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM