Initial upload: cmdparse CLI tool with comprehensive documentation and CI/CD
This commit is contained in:
85
cmdparse/cli.py
Normal file
85
cmdparse/cli.py
Normal file
@@ -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()
|
||||||
Reference in New Issue
Block a user