args.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. from Naked.commandline import Command
  4. from Naked.toolshed.system import exit_success
  5. import shlex
  6. class Args:
  7. def __init__(self, command_string):
  8. self.com_string = command_string
  9. def run(self):
  10. cmd_list = shlex.split(self.com_string)
  11. c = Command(cmd_list[0], cmd_list[1:])
  12. print(' ')
  13. print("•naked• Assuming that your Command object is instantiated as an instance named 'c', the command that you entered would be parsed as follows:")
  14. print(' ')
  15. print('Application')
  16. print('-----------')
  17. print('c.app = ' + c.app)
  18. print(' ')
  19. print('Argument List Length')
  20. print('--------------------')
  21. print('c.argc = ' + str(c.argc))
  22. print(' ')
  23. print('Argument List Items')
  24. print('-------------------')
  25. print('c.argobj = ' + str(c.argobj))
  26. print(' ')
  27. print('Arguments by Zero Indexed Start Position')
  28. print('----------------------------------------')
  29. print('c.arg0 = ' + c.arg0)
  30. print('c.arg1 = ' + c.arg1)
  31. print('c.arg2 = ' + c.arg2)
  32. print('c.arg3 = ' + c.arg3)
  33. print('c.arg4 = ' + c.arg4)
  34. print(' ')
  35. print('Arguments by Named Position')
  36. print('---------------------------')
  37. print('c.first = ' + c.first)
  38. print('c.second = ' + c.second)
  39. print('c.third = ' + c.third)
  40. print('c.fourth = ' + c.fourth)
  41. print('c.fifth = ' + c.fifth)
  42. print(' ')
  43. print('Last Positional Argument')
  44. print('------------------------')
  45. print('c.arglp = ' + c.arglp)
  46. print('c.last = ' + c.last)
  47. print(' ')
  48. print('Primary & Secondary Commands')
  49. print('----------------------------')
  50. print('c.cmd = ' + c.cmd)
  51. print('c.cmd2 = ' + c.cmd2)
  52. print(' ')
  53. print('Option Exists Tests')
  54. print('------------------')
  55. print('c.option_exists() = ' + str(c.option_exists()))
  56. print('c.options = ' + str(c.options))
  57. print(' ')
  58. print('Option Argument Assignment')
  59. print('--------------------------')
  60. if c.option_exists(): # if there are options, iterate through and print arguments to them
  61. non_flag_options = False
  62. for x in range(len(c.optobj)):
  63. if '=' in c.optobj[x]:
  64. continue # don't print flags, they are handled below
  65. else:
  66. print('c.arg("' + c.optobj[x] + '") = ' + c.arg(c.optobj[x]))
  67. non_flag_options = True
  68. if not non_flag_options:
  69. print("There are no short or long options in the command.")
  70. else: # otherwise, inform user that there are no options
  71. print("There are no short options, long options, or flags in your command.")
  72. print(' ')
  73. print('Flag Exists Tests')
  74. print('----------------')
  75. print('c.flag_exists() = ' + str(c.flag_exists()))
  76. print('c.flags = ' + str(c.flags))
  77. print(' ')
  78. print('Flag Argument Assignment')
  79. print('------------------------')
  80. if c.flag_exists():
  81. for y in c.optobj:
  82. if '=' in y:
  83. the_flag = y.split('=')[0]
  84. print('c.flag_arg("' + the_flag + '") = ' + c.flag_arg(the_flag))
  85. else: # provide message if there are no flags
  86. print("There are no flag style arguments (--flag=argument) in your command.")
  87. exit_success()
  88. #------------------------------------------------------------------------------
  89. # [ help function ] - help for the where command
  90. #------------------------------------------------------------------------------
  91. def help():
  92. from Naked.toolshed.system import exit_success
  93. help_string = """
  94. Naked args Command Help
  95. =======================
  96. The args command displays information about the data that are parsed from a command string to Command object attributes and that are obtained from Command object methods. It is intended to help with the design of your application logic when you use the Naked command line parser.
  97. USAGE
  98. naked args '<command statement>'
  99. The command statement is a mandatory argument to the command. It should include a complete command as it would be entered on the command line, including the executable. The argument should be completely enclosed within quotes.
  100. SECONDARY COMMANDS
  101. none
  102. OPTIONS
  103. none
  104. EXAMPLE
  105. naked args 'testapp save somestring --unicode -s --name=file.txt'"""
  106. print(help_string)
  107. exit_success()
  108. if __name__ == '__main__':
  109. pass