callingconvention
callingconvention

Reputation: 193

argparse subparser help display

I can't seem to get the subparser help commands to properly display their help. if I do command.py -h, I get the full help menu, but the subcommand/subparser help isn't showing.

When I typed command.py import -h, I wanted the help screen for the import subcommand and not the parent help.

I can't seem to figure out what is missing/broken.

parser = argparse.ArgumentParser(description = 'description', epilog='some epilog', add_help=True)
parser.add_argument('username', help='username')
parser.add_argument('apikey', help='API key')
parser.add_argument('--host', default=host, help='set the API host.  Defaults to %s' % host)
parser.add_argument('--port', default=port, type=int, help='set the API port.  Defaults to %s' % port)
parser.add_argument('--tls', dest='tls', action='store_true', help='set TLS (default value)')
parser.add_argument('--notls', dest='tls', action='store_false', help='turn off TLS')
parser.add_argument('--proxy', help='proxy url example: https://username:[email protected]:8080')
parser.add_argument('--type', default='https', choices=['http', 'https'], help='set proxy type')
parser.set_defaults(tls=True)
subparsers = parser.add_subparsers(title='subcommands', description='valid subcommands', help='sub-command help')
search_subparser = subparsers.add_parser('search', help='search help')
search_subparser.add_argument('--itype', choices=valid_intel_type, help='restrict by intelligence type')
search_subparser.add_argument('--severity', choices=valid_severity, help='restrict by severity')
search_subparser.add_argument('--classification', choices=valid_classification, help='restrict by classification')
search_subparser.add_argument('--status', choices=valid_status, help='parse by status')
search_subparser.add_argument('--output', choices=['json', 'csv'], default=output, help='output format.  Defaults to %s' % output)
import_subparser = subparsers.add_parser('import', help='import help')
import_subparser.add_argument('file', help='file of indicators to import')
import_subparser.add_argument('itype', choices=valid_intel_type, help='intelligence type to import')
import_subparser.add_argument('--classification', default='private', choices=valid_classification, help='set classification level for import')
import_subparser.add_argument('--confidence', default=30, type=int, help='set cconfidence level for import')
import_subparser.add_argument('--severity', default='medium', choices=valid_severity, help='set severity level for import')
import_subparser.add_argument('--notes', default=None, nargs='*', help='set analyst notes for import file')
args = parser.parse_args()

Upvotes: 3

Views: 2445

Answers (1)

callingconvention
callingconvention

Reputation: 193

The answer, for anyone else who uses multiple positional arguments, is that you have to supply the positional arguments along with the subcommand.

in my case:

command.py <username> <apikey> search -h

or:

command.py <username> <apikey> import -h

Upvotes: 4

Related Questions