string_cli.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import sys
  2. import commands
  3. def print_help():
  4. help_text = '''
  5. Usage: python string_cli.py <command> <string>
  6. Commands:
  7. reverse - Reverses the input string.
  8. uppercase - Converts the input string to uppercase.
  9. lowercase - Converts the input string to lowercase.
  10. spongebob - Converts the input string to spongebob case.
  11. length - Returns the length of the input string.
  12. scramble - Randomly scrambles the characters in the input string.
  13. '''
  14. print(help_text)
  15. if __name__ == '__main__':
  16. if len(sys.argv) == 2 and sys.argv[1] == '--help':
  17. print_help()
  18. sys.exit(0)
  19. elif len(sys.argv) < 3:
  20. print('Usage: python string_cli.py <command> <string>')
  21. sys.exit(1)
  22. command = sys.argv[1]
  23. input_string = sys.argv[2]
  24. if command == 'reverse':
  25. from commands.reverse import reverse_string
  26. print(reverse_string(input_string))
  27. elif command == 'uppercase':
  28. from commands.uppercase import to_uppercase
  29. print(to_uppercase(input_string))
  30. elif command == 'lowercase':
  31. from commands.lowercase import to_lowercase
  32. print(to_lowercase(input_string))
  33. elif command == 'spongebob':
  34. from commands.spongebob import spongebob_case
  35. print(spongebob_case(input_string))
  36. elif command == 'length':
  37. from commands.length import string_length
  38. print(string_length(input_string))
  39. elif command == 'scramble':
  40. from commands.scramble import scramble_string
  41. print(scramble_string(input_string))
  42. else:
  43. print('Invalid command!')