1. import argparse
  2. import sys
  3. def collect_cli_metrics():
  4. """Collects and prints metrics about command-line arguments."""
  5. parser = argparse.ArgumentParser(description="Collect CLI argument metrics.")
  6. # Define arguments (example)
  7. parser.add_argument("--input_file", type=str, help="Path to input file")
  8. parser.add_argument("--output_dir", type=str, help="Path to output directory")
  9. parser.add_argument("--dry_run", action="store_true", help="Perform a dry run")
  10. parser.add_argument("--verbose", action="store_true", help="Enable verbose output")
  11. args = parser.parse_args()
  12. # Count the number of arguments
  13. num_args = len(sys.argv) - 1 # Exclude the script name
  14. # Count the number of arguments with specific names.
  15. input_file_count = 0
  16. output_dir_count = 0
  17. dry_run_count = 0
  18. verbose_count = 0
  19. for arg in args:
  20. if arg == "--input_file":
  21. input_file_count += 1
  22. elif arg == "--output_dir":
  23. output_dir_count += 1
  24. elif arg == "--dry_run":
  25. dry_run_count += 1
  26. elif arg == "--verbose":
  27. verbose_count += 1
  28. print("CLI Argument Metrics:")
  29. print(f" Total arguments: {num_args}")
  30. print(f" --input_file: {input_file_count}")
  31. print(f" --output_dir: {output_dir_count}")
  32. print(f" --dry_run: {dry_run_count}")
  33. print(f" --verbose: {verbose_count}")
  34. if __name__ == "__main__":
  35. collect_cli_metrics()

Add your comment