Nested commands
Advanced Features and Best Practices
1. Command Inheritance
You can create a base command class to share functionality:
class BaseCommand(Parser):
def common_method(self):
pass
class SpecificCommand(BaseCommand):
def cli_run(self):
self.common_method()
2. Argument Inheritance
Global arguments are accessible to subcommands:
class AppMain(Parser):
verbose = Argument("--verbose", action="store_true")
class SubCommand(Parser):
def cli_run(self, verbose=False, **_):
if verbose:
print("Verbose mode enabled")
3. Custom Help Messages
Override the default help behavior:
def cli_run(self, **_):
print("Custom usage information:")
self.show_usage()
print("\nDetailed help:")
self.show_help()
4. Command Organization Best Practices
-
Logical Grouping:
- Group related commands under common parents
- Use meaningful command names
- Keep the hierarchy shallow (3-4 levels max)
-
Argument Design:
- Put shared options in parent commands
- Use consistent naming across commands
- Provide sensible defaults
-
Documentation:
- Write clear help messages
- Document command relationships
- Include examples in docstrings
-
Code Structure:
- One class per command
- Use inheritance for shared behavior
- Keep command implementations focused
Error Handling and Validation
- Command Not Found:
def cli_run(self, **_):
if not hasattr(self, 'subcommand'):
print("No command specified")
self.show_help()
return 1
- Argument Validation:
Testing Nested Commands
- Test Command Structure:
def test_command_structure():
app = AppMain()
assert hasattr(app, 'command1')
assert hasattr(app.command1, 'sub1')
- Test Command Execution: