Related Code Files:
scripts/python_tools/enhanced_parsers.py- Core enhanced parser implementationscripts/python_tools/find_text.py- Example search tool using enhanced parserscripts/python_tools/smart_ls.py- Example directory tool using enhanced parserscripts/python_tools/method_analyzer_ast.py- Example analysis tool using enhanced parser
The Enhanced Parser System provides standardized argument parsing for Python development tools, ensuring consistency across the entire toolkit. It offers three specialized parser types (search, analyze, directory) with common arguments, automatic conflict resolution, and enterprise-grade features.
- Consistency: All tools use the same argument patterns
- No Conflicts: Automatic resolution of argparse partial matching issues
- Enterprise Features: Built-in logging, error handling, and security
- Easy Migration: Drop-in replacement for standard argparse
Used for tools that search for patterns in files.
Standard Arguments:
# Core search arguments
--file FILE # Search in specific file
--scope SCOPE # Search in directory/scope
--ext EXT # Filter by extension
-g, --glob PATTERN # Glob pattern filter
-r, --recursive # Recursive search (default: True)
--no-recursive # Disable recursive search
# Output control
-v, --verbose # Verbose output
-q, --quiet # Quiet mode
--json # JSON output format
--output FILE # Write output to file
# Common options
--threads N # Parallel processing threads
--timeout SECONDS # Operation timeout
--limit N, --max N # Limit results (with alias)Example Usage:
from enhanced_parsers import create_enhanced_parser
parser = create_enhanced_parser(
'search',
description='Search for text patterns in files'
)
# Add tool-specific arguments
parser.add_argument('pattern', help='Pattern to search for')
parser.add_argument('-i', '--ignore-case', action='store_true',
help='Case insensitive search')
args = parser.parse_args()Used for tools that analyze code structure, dependencies, or quality.
Standard Arguments:
# Core analysis arguments
TARGET # What to analyze (file/method/class)
--file FILE # Analyze specific file
--scope SCOPE # Analyze directory/scope
--type TYPE # Analysis type (method/class/function)
# Output control
-v, --verbose # Verbose output
-q, --quiet # Quiet mode
--json # JSON output format
--output FILE # Write output to file
# Analysis options
--detailed # Detailed analysis
--ast-context # Show AST context
--no-ast-context # Disable AST context
--threads N # Parallel processing
--timeout SECONDS # Operation timeoutExample Usage:
parser = create_enhanced_parser(
'analyze',
description='Analyze code structure and dependencies'
)
# Add analysis-specific arguments
parser.add_argument('--depth', type=int, default=3,
help='Analysis depth')
parser.add_argument('--show-imports', action='store_true',
help='Show import analysis')
args = parser.parse_args()Used for tools that operate on directories and files.
Standard Arguments:
# Core directory arguments
PATH # Directory path (positional)
--ext EXT # Filter by extension
-g, --glob PATTERN # Glob pattern filter
-r, --recursive # Recursive operation
--no-recursive # Disable recursive
# Output control
-v, --verbose # Verbose output
-q, --quiet # Quiet mode
--json # JSON output format
--output FILE # Write output to file
# Directory options
--limit N, --max N # Limit results (with alias)
--sort FIELD # Sort by field
--reverse # Reverse sort order
--size-format FORMAT # Size display format
--show-hidden # Include hidden filesExample Usage:
parser = create_enhanced_parser(
'directory',
description='Enhanced directory listing tool'
)
# Add directory-specific arguments
parser.add_argument('--group-by', choices=['ext', 'dir', 'size'],
help='Group results by criteria')
parser.add_argument('--min-size', type=str,
help='Minimum file size (e.g., 1M, 100K)')
args = parser.parse_args()Determine which parser type fits your tool:
- Search: Tools that find patterns in files
- Analyze: Tools that analyze code structure
- Directory: Tools that work with files/directories
# Old code
import argparse
parser = argparse.ArgumentParser(description='My tool')
# New code
from enhanced_parsers import create_enhanced_parser
parser = create_enhanced_parser('search', description='My tool')Check for conflicts with standard arguments:
# If you had custom --limit argument
parser.add_argument('--limit', ...) # Remove this
# Use the standard limit/max instead
# It's already included with alias support# Add only arguments unique to your tool
parser.add_argument('--my-special-option',
help='Tool-specific functionality')# Test standard arguments work
./tool.py --help
./tool.py pattern --file test.py --verbose
./tool.py pattern --scope src/ --limit 10
./tool.py pattern --scope src/ --max 10 # Alias should workThe enhanced parser automatically adds aliases for common conflicts:
# --limit automatically gets --max alias
# Users can use either:
./tool.py --limit 10
./tool.py --max 10Some arguments are excluded from certain parser types:
# Directory parser doesn't include:
# --type (conflicts with file type filtering)
# --ast-context (not applicable to directory ops)Override standard arguments if needed:
parser = create_enhanced_parser('search', description='My tool')
# Remove standard argument
for action in parser._actions:
if '--threads' in action.option_strings:
parser._remove_action(action)
break
# Add custom version
parser.add_argument('--threads', type=int, default=1,
help='Number of worker threads (max 4)')#!/usr/bin/env python3
"""Enhanced text search tool."""
import sys
from enhanced_parsers import create_enhanced_parser
def main():
parser = create_enhanced_parser(
'search',
description='Search for text patterns with context'
)
# Add search-specific arguments
parser.add_argument('pattern', help='Pattern to search for')
parser.add_argument('-i', '--ignore-case', action='store_true',
help='Case insensitive search')
parser.add_argument('-C', '--context', type=int, default=0,
help='Lines of context')
args = parser.parse_args()
# Use standard arguments
if args.file:
search_file(args.file, args.pattern, args)
elif args.scope:
search_directory(args.scope, args.pattern, args)
else:
parser.error("Either --file or --scope required")
def search_file(filepath, pattern, args):
"""Search in a single file."""
# Implementation using args.verbose, args.json, etc.
pass
def search_directory(directory, pattern, args):
"""Search in directory."""
# Use args.recursive, args.glob, args.ext, etc.
pass
if __name__ == '__main__':
main()#!/usr/bin/env python3
"""Code structure analyzer."""
from enhanced_parsers import create_enhanced_parser
def main():
parser = create_enhanced_parser(
'analyze',
description='Analyze code structure and dependencies'
)
# Target is already added as positional
parser.add_argument('--show-calls', action='store_true',
help='Show method calls')
parser.add_argument('--max-depth', type=int, default=3,
help='Maximum analysis depth')
args = parser.parse_args()
# Analyze based on args.type
if args.type == 'method':
analyze_method(args.target, args)
elif args.type == 'class':
analyze_class(args.target, args)
if __name__ == '__main__':
main()#!/usr/bin/env python3
"""Enhanced directory operations."""
from enhanced_parsers import create_enhanced_parser
def main():
parser = create_enhanced_parser(
'directory',
description='Smart directory listing and analysis'
)
# Path is already added as positional
parser.add_argument('--min-size', type=str,
help='Minimum file size (e.g., 1M)')
parser.add_argument('--max-size', type=str,
help='Maximum file size (e.g., 10M)')
args = parser.parse_args()
# List directory with filters
list_directory(args.path, args)
def list_directory(path, args):
"""List directory contents."""
# Use args.ext, args.glob, args.recursive, etc.
# Use args.limit or args.max (same thing due to alias)
pass
if __name__ == '__main__':
main()# Always set version for tracking
parser = create_enhanced_parser(
'search',
description='My search tool',
version='2.0.0' # Optional but recommended
)# Validate combinations in parse_args override
class MyParser(EnhancedArgumentParser):
def parse_args(self, args=None, namespace=None):
args = super().parse_args(args, namespace)
# Custom validation
if args.file and args.scope:
self.error("Cannot use both --file and --scope")
return args# Set sensible defaults for your tool
parser.set_defaults(
recursive=True, # Override standard default
threads=4, # Tool-specific default
ast_context=True # Enable by default
)# Add examples to help
parser.epilog = """
Examples:
%(prog)s "TODO" --file main.py
%(prog)s "import.*" --scope src/ --type regex
%(prog)s "def.*test" --scope tests/ -r
"""# Use consistent exit codes
def main():
try:
# ... tool logic ...
return 0 # Success
except KeyboardInterrupt:
return 130 # Ctrl+C
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
return 1 # General errorSymptom: ArgumentError: argument --limit: conflicting option string
Solution: The enhanced parser handles common conflicts. Check if:
- You're using the right parser type
- You're not manually adding standard arguments
- You need to exclude the argument first
Symptom: --ma matches both --max and --match
Solution: Enhanced parser disables partial matching by default. Users must type full argument names or use short versions.
Symptom: --ast-context not available in directory tool
Solution: Some arguments are excluded by parser type. Check the parser type's standard arguments list.
Symptom: ImportError: cannot import name 'create_enhanced_parser'
Solution: Ensure enhanced_parsers.py is in your Python path:
export PYTHONPATH=/path/to/scripts/python_tools:$PYTHONPATHSymptom: Different behavior in different environments
Solution: Check Python version (requires 3.6+) and ensure latest enhanced_parsers.py version.
Create your own parser type:
def create_custom_parser():
parser = EnhancedArgumentParser()
# Add your standard arguments
parser.add_argument('--custom-arg', help='Custom standard arg')
# Set parser type for identification
parser.parser_type = 'custom'
return parserEnhanced parsers automatically respect .pytoolsrc settings:
[DEFAULT]
verbose = true
ast_context = true
threads = 8- Use
--threadsfor parallel operations - Set reasonable
--timeoutvalues - Use
--limitto cap results in large operations
The Enhanced Parser System provides a robust foundation for Python development tools with:
- Standardized argument patterns across all tools
- Automatic conflict resolution
- Enterprise-grade features
- Easy migration path
By following this guide, you can create consistent, professional tools that integrate seamlessly with the existing toolkit while avoiding common argparse pitfalls.