From f1bb30ed94347b791b353e5b9888569f129bd48f Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Wed, 4 Feb 2026 02:08:44 +0000 Subject: [PATCH] Initial upload: cmdparse CLI tool with comprehensive documentation and CI/CD --- cmdparse/cli.py | 85 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 cmdparse/cli.py diff --git a/cmdparse/cli.py b/cmdparse/cli.py new file mode 100644 index 0000000..d94f03a --- /dev/null +++ b/cmdparse/cli.py @@ -0,0 +1,85 @@ +"""Main CLI entry point and argument parsing using Click.""" + +import sys +import click +from typing import List, Optional + +from .parser import parse_text +from .extractors import extract_fields +from .formatters import format_data +from .config import load_config, get_all_parsers + + +@click.command() +@click.option( + '--output', '-o', + type=click.Choice(['json', 'yaml', 'csv', 'raw']), + default='json', + help='Output format (default: json)' +) +@click.option( + '--format', '-f', + type=click.Choice(['json', 'yaml', 'csv', 'raw', 'auto']), + default='auto', + help='Input format hint (auto-detect by default)' +) +@click.option( + '--field', '-e', + multiple=True, + help='Fields to extract (supports dot notation)' +) +@click.option( + '--config', '-c', + type=click.Path(exists=True, readable=True), + help='Path to custom config file' +) +@click.option( + '--quiet', '-q', + is_flag=True, + help='Suppress pattern detection info' +) +@click.argument( + 'input_file', + type=click.File('r'), + default='-' +) +def main( + output: str, + format: str, + field: tuple, + config: Optional[str], + quiet: bool, + input_file +) -> None: + """Parse unstructured CLI output into structured formats.""" + try: + text = input_file.read() + + if not text or not text.strip(): + click.echo("Error: No input provided", err=True) + sys.exit(1) + + pattern_type = None + if format != 'auto': + pattern_type = format + + data, detected = parse_text(text, pattern_type) + + if data and not quiet: + click.echo(f"Detected pattern: {detected}", err=True) + + if field: + fields = list(field) + data = extract_fields(data, fields) + + result = format_data(data, output) + + click.echo(result) + + except Exception as e: + click.echo(f"Error: {e}", err=True) + sys.exit(1) + + +if __name__ == '__main__': + main()