NRLMMD-GEOIPS / geoips

Main Geolocated Information Processing System code base with basic functionality enabled.
https://nrlmmd-geoips.github.io/geoips/
Other
13 stars 10 forks source link

Add functionality to the CLI which prints out a tree of available commands #627

Open evrose54 opened 3 weeks ago

evrose54 commented 3 weeks ago

Requested Update

Description

After #465 is merged into main, we should consider updating the CLI to support functionality which can map out all available commands. For example, if we were to run geoips commands (or something along those lines), this would produce a tree of available commands which you can run via the CLI. This doesn't need to be done immediately but would be a very useful utility that people could make use of to easily understand the CLI. We should consider adding a --max-depth <num> to limit how far we traverse down this tree.

Background and Motivation

This stems from this comment on #465.

Code to demonstrate issue

Here is some pseudo code which demonstrates what we'd need to do in the CLI

import argparse

def print_tree(parser, level=0):
    indent = ' ' * (level * 4)
    print(f"{indent}{parser.prog.split()[-1]}")
    if hasattr(parser, '_subparsers'):
        subparsers_action = parser._subparsers._group_actions[0]
        for choice, subparser in subparsers_action.choices.items():
            print_tree(subparser, level + 1)

def main():
    parser = argparse.ArgumentParser(prog='geoips')
    subparsers = parser.add_subparsers(dest='command')

    # Create the 'get' command and its subcommands
    get_parser = subparsers.add_parser('get')
    get_subparsers = get_parser.add_subparsers(dest='subcommand')
    get_subparsers.add_parser('subcommand1')
    get_subparsers.add_parser('subcommand2')

    # Create the 'list' command and its subcommands
    list_parser = subparsers.add_parser('list')
    list_subparsers = list_parser.add_subparsers(dest='subcommand')
    list_subparsers.add_parser('subcommandA')
    list_subparsers.add_parser('subcommandB')

    # Create the 'test' command and its subcommands
    test_parser = subparsers.add_parser('test')
    test_subparsers = test_parser.add_subparsers(dest='subcommand')
    test_subparsers.add_parser('subcommandX')
    test_subparsers.add_parser('subcommandY')

    # Print the command tree
    print_tree(parser)

    args = parser.parse_args()
    print(args)

if __name__ == '__main__':
    main()