Python argparse order for sub-command and defaults values

269 Views Asked by At

I have been struggling with it for a long time, so I would like to do sub-commands for another command which will be ignore order opposite to subparsers = parser.add_subparsers(), because a subparser can be executed at the end of arguments or if it is alone.

For example test.py is connecting to the device via SSH and execute some command, I can see output in the terminal but like I'd like to save it e.g. as filename_test_1 (default name is command_output) as .txt or .csv (default format is txt).

I wish I could make --save in various possible ways (keeping the -n and -f executed only after --save --> for example error: argument -f/--format: can't be used before --save):

python3 test.py --save
python3 test.py --save -n file_name
python3 test.py --save -n file_name -f csv
python3 test.py --save -n file_name --command "cat /etc/passwd"
python3 test.py --command "cat /etc/passwd" --save -f csv

I am basing on this code, but I can't rewrite it into my own needs. One of the problem is --save is not store_true argument:

#!/usr/bin/python3

import argparse
from collections import OrderedDict

def open_gui():
    print("GUI has been opened.")
    return 0

def test_information():
    print("Some information.")
    return 0

class ParentAction(argparse.Action):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, default=OrderedDict(), **kwargs)

        self.children = []

    def __call__(self, parser, namespace, values, option_string=None):
        items = getattr(namespace, self.dest)
        nspace = type(namespace)()
        for child in self.children:
            if child.default is not None:
                setattr(nspace, child.name, child.default)
        items[values] = nspace

class ChildAction(argparse.Action):
    def __init__(self, *args, parent, sub_action='store', **kwargs):
        super().__init__(*args, **kwargs)

        self.dest, self.name = parent.dest, self.dest
        self.action = sub_action
        self._action = None
        self.parent = parent

        parent.children.append(self)

    def get_action(self, parser):
        if self._action is None:
            action_cls = parser._registry_get('action', self.action, self.action)
            self._action = action_cls(self.option_strings, self.name)
        return self._action

    def __call__(self, parser, namespace, values, option_string=None):
        items = getattr(namespace, self.dest)
        try:
            last_item = next(reversed(items.values()))
        except StopIteration:
            raise argparse.ArgumentError(self, "can't be used before {}".format(self.parent.option_strings[0]))
        action = self.get_action(parser)
        action(parser, last_item, values, option_string)

def main(command_line=None):

    print('')

    parser = argparse.ArgumentParser(prog='SSHtest',
        description='It is example description',
        add_help=False)

    single_group = parser.add_argument_group('single use arguments')
    single_group_exception = parser.add_argument_group('exception for single use arguments')
    multi_group = parser.add_argument_group('multiple use arguments')

    single_group.add_argument('--help', '-h', action='help', help='show this help message and exit')
    single_group.add_argument('--version', '-v', action='version', version='%(prog)s 1.0.1a201009')
    single_group.add_argument('--information', '-i', action='store_true', help='show alias information and exit')
    single_group.add_argument('--gui', action='store_true', help='open a GUI application from default web browser and exit')

    multi_group.add_argument('--debug', action='store_true', help='print debug info')
    multi_group.add_argument('--command', type=str, help='execute custom command and exit')

    parent = parser.add_argument('-s', '--save', action=ParentAction)
    parser.add_argument('-n', '--name', action=ChildAction, parent=parent, default='command_output', metavar='NAME', help='set file name (default: %(default)s)')
    parser.add_argument('-f', '--format', choices=['txt', 'csv', 'xlsx'], default='txt', action=ChildAction, parent=parent, metavar='FORMAT', help='set format of file, default is %(default)s (%(choices)s)')

    args = parser.parse_args(command_line)

    if args.information:
        test_information()
        parser.exit()

    if args.debug:
        print("debug: " + str(args))

    if args.gui:
        open_gui()

if __name__ == '__main__':
    main()

maybe there is a simple way? I would also like to keep "good" formatting of --help

Note: I would like to use basic libs so I'll stay with argparse :v

0

There are 0 best solutions below