1. import hashlib
  2. import argparse
  3. import sys
  4. def hash_cli_args(args):
  5. """Hashes the arguments to enable dry-run comparison."""
  6. stringified_args = str(args) # Convert args to a string representation
  7. hashed_args = hashlib.sha256(stringified_args.encode('utf-8')).hexdigest() # Hash the string
  8. return hashed_args
  9. if __name__ == "__main__":
  10. parser = argparse.ArgumentParser() # Create an argument parser
  11. # Add arguments
  12. parser.add_argument("--param1", type=str, default="default_value", help="First parameter")
  13. parser.add_argument("--param2", type=int, default=10, help="Second parameter")
  14. parser.add_argument("--flag", action="store_true", help="A flag")
  15. args = parser.parse_args() # Parse arguments
  16. hashed_value = hash_cli_args(args) # Hash the arguments
  17. print(f"Hashed arguments: {hashed_value}") # Print the hashed value
  18. # Example usage: Compare hashed values for dry-run
  19. if len(sys.argv) > 1:
  20. test_args = parser.parse_args(sys.argv[1:])
  21. test_hashed = hash_cli_args(test_args)
  22. if hashed_value == test_hashed:
  23. print("Arguments are the same (dry-run)")
  24. else:
  25. print("Arguments are different")

Add your comment