Scaling Your Python CLI

How to maker your programs modular

As our programs grow we often need to go back to the drawing board

Our CLI

V1 Project Layout

├── main.py
├── setup.py
Our general click hierarch

V1.5 Trying To be Modular

V2 Modular Project Layout

├── main.py
├── setup.py
└── src
├── directories
│ └── directory.py
└── files
└── file.py

Structuring Your Commands

Noun First — Cli File Create

Noun First Project
cli file
Usage: cli file [OPTIONS] COMMAND [ARGS]...
Options:
--help Show this message and exit.
Commands:
create
delete
get

Verb First — Cli Create File

def merge_commands(group, command_collection:CommandCollection):    """ Set the group's commands to those in the collection"""    new_commands = {}
command_sources = command_collection.sources
for group in command_sources:
for command_name,command in group.commands.items():
new_commands[command_name] = command

group.commands = new_commands
return group
cli create
Usage: cli create [OPTIONS] COMMAND [ARGS]...
Options:
--help Show this message and exit.
Commands:
directory Create directory
file Create file

Conclusion

--

--

ML Lead @ Voiceflow

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store