function parse_arguments
Parses command-line arguments for a contract validity analysis tool that processes FileCloud documents with configurable options for paths, concurrency, output, and file filtering.
/tf/active/vicechatdev/contract_validity_analyzer/main.py
20 - 85
simple
Purpose
This function sets up and parses command-line arguments for a contract analysis application. It provides a comprehensive CLI interface allowing users to customize configuration file paths, FileCloud document locations, processing concurrency, output directories, logging verbosity, dry-run mode, file extension filtering, and file processing limits. The function uses argparse to create a user-friendly interface with help text and usage examples.
Source Code
def parse_arguments():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
description='Analyze contract validity from FileCloud documents',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python main.py # Run with default configuration
python main.py --config custom_config.yaml # Use custom configuration file
python main.py --path "/custom/path" # Override FileCloud path
python main.py --concurrent 5 # Use 5 concurrent threads
python main.py --output-dir ./results # Save results to custom directory
"""
)
parser.add_argument(
'--config', '-c',
help='Path to configuration file',
default=None
)
parser.add_argument(
'--path', '-p',
help='FileCloud path to analyze (overrides config)',
default=None
)
parser.add_argument(
'--concurrent', '-t',
help='Number of concurrent processing threads',
type=int,
default=3
)
parser.add_argument(
'--output-dir', '-o',
help='Output directory for results',
default=None
)
parser.add_argument(
'--verbose', '-v',
help='Enable verbose logging',
action='store_true'
)
parser.add_argument(
'--dry-run',
help='Show what would be analyzed without actually processing',
action='store_true'
)
parser.add_argument(
'--extensions',
help='File extensions to analyze (comma-separated)',
default='.pdf,.doc,.docx'
)
parser.add_argument(
'--max-files',
help='Maximum number of files to process (for testing)',
type=int,
default=None
)
return parser.parse_args()
Return Value
Returns an argparse.Namespace object containing parsed command-line arguments with the following attributes: config (str or None) - path to configuration file, path (str or None) - FileCloud path to analyze, concurrent (int, default 3) - number of concurrent threads, output_dir (str or None) - output directory path, verbose (bool) - verbose logging flag, dry_run (bool) - dry-run mode flag, extensions (str, default '.pdf,.doc,.docx') - comma-separated file extensions, max_files (int or None) - maximum files to process
Dependencies
argparse
Required Imports
import argparse
Usage Example
import argparse
def parse_arguments():
parser = argparse.ArgumentParser(
description='Analyze contract validity from FileCloud documents',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python main.py # Run with default configuration
python main.py --config custom_config.yaml # Use custom configuration file
python main.py --path "/custom/path" # Override FileCloud path
python main.py --concurrent 5 # Use 5 concurrent threads
python main.py --output-dir ./results # Save results to custom directory
"""
)
parser.add_argument('--config', '-c', help='Path to configuration file', default=None)
parser.add_argument('--path', '-p', help='FileCloud path to analyze (overrides config)', default=None)
parser.add_argument('--concurrent', '-t', help='Number of concurrent processing threads', type=int, default=3)
parser.add_argument('--output-dir', '-o', help='Output directory for results', default=None)
parser.add_argument('--verbose', '-v', help='Enable verbose logging', action='store_true')
parser.add_argument('--dry-run', help='Show what would be analyzed without actually processing', action='store_true')
parser.add_argument('--extensions', help='File extensions to analyze (comma-separated)', default='.pdf,.doc,.docx')
parser.add_argument('--max-files', help='Maximum number of files to process (for testing)', type=int, default=None)
return parser.parse_args()
# Usage in main script
if __name__ == '__main__':
args = parse_arguments()
print(f"Config file: {args.config}")
print(f"FileCloud path: {args.path}")
print(f"Concurrent threads: {args.concurrent}")
print(f"Output directory: {args.output_dir}")
print(f"Verbose mode: {args.verbose}")
print(f"Dry run: {args.dry_run}")
print(f"Extensions: {args.extensions}")
print(f"Max files: {args.max_files}")
Best Practices
- This function should be called once at the start of the main script to parse command-line arguments
- The returned Namespace object should be validated before use, especially for file paths and numeric values
- Consider wrapping the call in a try-except block to handle argparse.ArgumentError gracefully
- The extensions argument returns a string; split by comma to get individual extensions: args.extensions.split(',')
- Default values are provided for most arguments, making all options truly optional
- The --dry-run flag should be checked before performing any actual file processing or API calls
- The --max-files option is intended for testing and should be documented as such in production use
- Verbose mode should be used to configure logging levels in the application
- The concurrent threads value should be validated against system resources before use
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function parse_arguments_v1 93.0% similar
-
function main_v42 67.0% similar
-
function main_v4 65.2% similar
-
function main_v41 64.9% similar
-
function main 64.1% similar