1. import argparse
  2. def create_dry_run_parser(parser):
  3. """
  4. Adds dry-run options to an argparse parser with fallback logic.
  5. """
  6. # Define the dry-run option
  7. parser.add_argument(
  8. "--dry-run",
  9. action="store_true",
  10. help="Perform a dry run; do not make changes.",
  11. )
  12. # Define a fallback option
  13. parser.add_argument(
  14. "--safe",
  15. action="store_true",
  16. help="Perform a safe operation; checks for potential errors.",
  17. )
  18. #Example of handling a field with a default value and a dry-run override
  19. parser.add_argument(
  20. "--verbose",
  21. action="store_true",
  22. help="Enable verbose output.",
  23. default=False
  24. )
  25. return parser
  26. if __name__ == "__main__":
  27. parser = argparse.ArgumentParser(description="Example CLI with dry-run options.")
  28. parser = create_dry_run_parser(parser)
  29. args = parser.parse_args()
  30. if args.dry_run:
  31. print("Dry run mode enabled. No changes will be made.")
  32. elif args.safe:
  33. print("Safe mode enabled. Checking for potential errors.")
  34. else:
  35. print("Normal mode.")
  36. print(f"Verbose mode: {args.verbose}")

Add your comment