Building CLIs with argparser

May 13, 2024

sys.argv returns a list of arguments that have been inputted from stdin. The first argument in the list is the file path, and all remaining arguments are the user-supplied input (ie: sys.argv[1:]).

def main():
    args = _parse_args(sys.argv[1:])  # pass CLI arguments except file path
 
def _parse_args(args):
    parser = argparse.ArgumentParser(description="Run dispersion analysis and notify.")
    parser.add_argument("example_file", type=str, help="Path to the example file.")
    parser.add_argument("output_path", type=str, help="Path to the output directory.")
    return parser.parse_args()