This document serves as a guide for converting the existing argparse-based CLI to use Click.
-
Decorator-based Command Pattern
- Use
@click.command()
and@click.group()
decorators - Replace manual subparser creation with nested command groups
- Use
-
Option and Argument Handling
- Replace
add_argument()
with@click.option()
and@click.argument()
- Use Click's built-in type conversion and validation
- Replace
-
Command Groups
- Use
@click.group()
for organizing commands - Add subcommands with the
@group.command()
decorator
- Use
-
Parameter Handling
- Leverage built-in type validation and conversion
- Use
help
parameter for documentation - Set defaults with
default
parameter
-
Context Management
- Use Click's context to share state between commands
- Pass
obj
parameter for shared state
-
Create Basic Command Structure
@click.group() def cli(): """Abzu - Industry knowledge extraction.""" pass
-
Add Command Groups
@cli.group() def process(): """Processing commands.""" pass @process.command() @click.option("-i", "--input", default="data/semianalysis.jsonl", help="Input JSONL file path") def articles(input): """Process articles.""" # Implementation
-
Convert Option Specifications
- Replace
add_argument
calls with@click.option()
- Keep the same option names and help text
- Replace
-
Implement Entry Points
- Replace
main()
function with Click's command handling - Update imports for module-specific functions
- Replace
parser = argparse.ArgumentParser(description="Abzu - Industry knowledge extraction")
subparsers = parser.add_subparsers(dest="command", help="Commands")
# Process command
process_cmd = subparsers.add_parser("process", help="Processing commands")
process_subparsers = process_cmd.add_subparsers(dest="subcommand", help="Process subcommands")
# Articles subcommand
articles_cmd = process_subparsers.add_parser("articles", help="Process articles")
articles_cmd.add_argument("-i", "--input", default="data/semianalysis.jsonl")
@click.group()
def cli():
"""Abzu - Industry knowledge extraction."""
pass
@cli.group()
def process():
"""Processing commands."""
pass
@process.command()
@click.option("-i", "--input", default="data/semianalysis.jsonl",
help="Input JSONL file path (default: data/semianalysis.jsonl)")
def articles(input):
"""Process articles."""
from abzu.cli.process_articles import process_main
return process_main(input_file=input)
-
Rich Help Text
- Automatic help text formatting and display
- Use docstrings for command descriptions
-
Auto-completion
- Enable shell completion for commands and parameters
-
Echo Functions
- Use
click.echo()
instead of print for consistent output - Use
click.secho()
for colored output
- Use
-
Progress Bars
- Implement progress reporting with
click.progressbar()
- Implement progress reporting with
-
Prompts and Confirmations
- Use
click.prompt()
for user input - Use
click.confirm()
for yes/no questions
- Use