|  | 
|  | 1 | +#!/usr/bin/env python | 
|  | 2 | +""" | 
|  | 3 | +Command-line interface for the JSDoc parser library. | 
|  | 4 | +
 | 
|  | 5 | +This script provides a simple command-line interface for parsing and composing JSDoc strings. | 
|  | 6 | +""" | 
|  | 7 | + | 
|  | 8 | +import argparse | 
|  | 9 | +import json | 
|  | 10 | +import sys | 
|  | 11 | +from docstring_parser.parser import parse_jsdoc | 
|  | 12 | +from docstring_parser.composer import compose_jsdoc | 
|  | 13 | +from docstring_parser.utils import remove_jsdoc_component | 
|  | 14 | + | 
|  | 15 | + | 
|  | 16 | +def main(): | 
|  | 17 | +    """Entry point for the command-line interface.""" | 
|  | 18 | +    parser = argparse.ArgumentParser(description='Parse and compose JSDoc strings') | 
|  | 19 | +    subparsers = parser.add_subparsers(dest='command', required=True, help='Command to execute') | 
|  | 20 | +     | 
|  | 21 | +    # Parse command | 
|  | 22 | +    parse_parser = subparsers.add_parser('parse', help='Parse a JSDoc string into a JSON object') | 
|  | 23 | +    parse_parser.add_argument('file', type=str, nargs='?', help='File containing a JSDoc string (or use stdin)') | 
|  | 24 | +    parse_parser.add_argument('-o', '--output', type=str, help='Output file (default: stdout)') | 
|  | 25 | +     | 
|  | 26 | +    # Compose command | 
|  | 27 | +    compose_parser = subparsers.add_parser('compose', help='Compose a JSDoc string from a JSON object') | 
|  | 28 | +    compose_parser.add_argument('file', type=str, nargs='?', help='JSON file containing a JSDoc object (or use stdin)') | 
|  | 29 | +    compose_parser.add_argument('-o', '--output', type=str, help='Output file (default: stdout)') | 
|  | 30 | +     | 
|  | 31 | +    # Remove component command | 
|  | 32 | +    remove_parser = subparsers.add_parser('remove', help='Remove a component from a JSDoc object') | 
|  | 33 | +    remove_parser.add_argument('file', type=str, nargs='?', help='JSON file containing a JSDoc object (or use stdin)') | 
|  | 34 | +    remove_parser.add_argument('-t', '--type', type=str, required=True, | 
|  | 35 | +                           choices=['description', 'param', 'returns', 'throws', 'example', 'tag'], | 
|  | 36 | +                           help='Type of component to remove') | 
|  | 37 | +    remove_parser.add_argument('-i', '--identifier', type=str, | 
|  | 38 | +                           help='Identifier of the component (e.g., param name, tag name)') | 
|  | 39 | +    remove_parser.add_argument('-o', '--output', type=str, help='Output file (default: stdout)') | 
|  | 40 | +    remove_parser.add_argument('-f', '--format', type=str, choices=['json', 'jsdoc'], default='json', | 
|  | 41 | +                            help='Output format: json or jsdoc (default: json)') | 
|  | 42 | +     | 
|  | 43 | +    args = parser.parse_args() | 
|  | 44 | +     | 
|  | 45 | +    # Handle input | 
|  | 46 | +    input_data = '' | 
|  | 47 | +    if args.file: | 
|  | 48 | +        with open(args.file, 'r', encoding='utf-8') as f: | 
|  | 49 | +            input_data = f.read() | 
|  | 50 | +    else: | 
|  | 51 | +        input_data = sys.stdin.read() | 
|  | 52 | +     | 
|  | 53 | +    # Process commands | 
|  | 54 | +    if args.command == 'parse': | 
|  | 55 | +        try: | 
|  | 56 | +            result = parse_jsdoc(input_data) | 
|  | 57 | +            output = json.dumps(result, indent=2) | 
|  | 58 | +        except Exception as e: | 
|  | 59 | +            print(f"Error parsing JSDoc: {str(e)}", file=sys.stderr) | 
|  | 60 | +            sys.exit(1) | 
|  | 61 | +     | 
|  | 62 | +    elif args.command == 'compose': | 
|  | 63 | +        try: | 
|  | 64 | +            jsdoc_obj = json.loads(input_data) | 
|  | 65 | +            output = compose_jsdoc(jsdoc_obj) | 
|  | 66 | +        except json.JSONDecodeError: | 
|  | 67 | +            print("Error: Input is not valid JSON", file=sys.stderr) | 
|  | 68 | +            sys.exit(1) | 
|  | 69 | +        except Exception as e: | 
|  | 70 | +            print(f"Error composing JSDoc: {str(e)}", file=sys.stderr) | 
|  | 71 | +            sys.exit(1) | 
|  | 72 | +     | 
|  | 73 | +    elif args.command == 'remove': | 
|  | 74 | +        try: | 
|  | 75 | +            jsdoc_obj = json.loads(input_data) | 
|  | 76 | +            result = remove_jsdoc_component(jsdoc_obj, args.type, args.identifier) | 
|  | 77 | +             | 
|  | 78 | +            if args.format == 'json': | 
|  | 79 | +                output = json.dumps(result, indent=2) | 
|  | 80 | +            else:  # jsdoc format | 
|  | 81 | +                output = compose_jsdoc(result) | 
|  | 82 | +        except json.JSONDecodeError: | 
|  | 83 | +            print("Error: Input is not valid JSON", file=sys.stderr) | 
|  | 84 | +            sys.exit(1) | 
|  | 85 | +        except Exception as e: | 
|  | 86 | +            print(f"Error removing component: {str(e)}", file=sys.stderr) | 
|  | 87 | +            sys.exit(1) | 
|  | 88 | +     | 
|  | 89 | +    # Handle output | 
|  | 90 | +    if args.output: | 
|  | 91 | +        with open(args.output, 'w', encoding='utf-8') as f: | 
|  | 92 | +            f.write(output) | 
|  | 93 | +    else: | 
|  | 94 | +        print(output) | 
|  | 95 | + | 
|  | 96 | + | 
|  | 97 | +if __name__ == '__main__': | 
|  | 98 | +    main() | 
0 commit comments