diff --git a/app/cmdparse/cli.py b/app/cmdparse/cli.py new file mode 100644 index 0000000..860efcf --- /dev/null +++ b/app/cmdparse/cli.py @@ -0,0 +1,82 @@ +import sys +import click +from typing import Optional + +from .parser import parse_text +from .extractors import extract_fields +from .formatters import format_data + + +@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()