简体   繁体   English

Argparse:具有store_true标志的可选子解析器

[英]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. 如果我的术语不正确,请原谅我,但我正在寻找一种向可选的argparse参数添加子解析器的方法,每个参数均带有store_true标志。

Ideally, I'd like to use the following syntax to reference the boolean value of --html in the shodan_parser subparser: 理想情况下,我想使用以下语法在shodan_parser解析器中引用--html的布尔值:

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 您需要将开关从“ -h”更改为其他名称(或禁用帮助),因为“ -h”开关已被帮助菜单使用

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? 这个--shodan可选(标志)是否与子解析器名称有关? 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. 同时使用nargs='+'的位置和nargs='+'解析器可能会起作用,但可能会造成混乱。 When does the list of targets end, and the subparser (and its arguments) begin? targets列表何时结束,子解析器(及其参数)开始?

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 . 我猜测这将设置targets=['google.ca']shodan=True But you haven't given it a subparser command. 但是您没有给它一个subparser命令。 The --html is an unknown. --html是未知的。

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

Now you set shodan=True (the main parser flag). 现在,您将shodan=True设置为主解析器标志。 Again --html is unknown. 同样,-- --html是未知的。 It sets target=['google.ca'] . 它设置了target=['google.ca'] But now it tries to interpret askjeeves.ca as a subparser command . 但是现在,它尝试将askjeeves.ca解释为子解析器命令。 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. 继续,在主解析器中包括--html 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. 至于获取args.shodan.html ,这是一个更高级的问题,涉及名称空间的嵌套。 For now be happy if you get args.html . 现在,如果您获得args.html就感到高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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