Search code examples
pythondatabaseparsingcommandcommand-line-interface

Python: what are my options to parse complex commands with sequences of arguments and options shaped in a tree-like manner, with optional parameters?


I'm writing a Python CLI for a program that will insert entries in a database. The command structure is appended below; each box represents either an argument or an option. Do I have no other choice other than to manually make parsers for each branch, or is there a library that could allow me to ease the process of making these commands?

The reason the diagram is complicated is because it's the only way to fully determine a particular database entry. Any tips and suggestions are very welcome! enter image description here


Solution

  • If you're using python 3 you can use the argparse library 1. It's built-in.

    It supports sequences, optional parameters, type checking and layers of subcommands for your hierarchical needs. You can let it parse the command line when starting the python script or provide your own command line dynamically while the program is running.

    It provides an output that should be easy to map to your application logic.