app_file.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # VARS = app_name, developer, license_name, year
  4. app_file_string = """
  5. #!/usr/bin/env python
  6. # encoding: utf-8
  7. #------------------------------------------------------------------------------
  8. # {{app_name}}
  9. # Copyright {{year}} {{developer}}
  10. # {{license}}
  11. #------------------------------------------------------------------------------
  12. #------------------------------------------------------------------------------------
  13. # c.cmd = Primary command ({{app_name}} <primary command>)
  14. # c.cmd2 = Secondary command ({{app_name}} <primary command> <secondary command>)
  15. #
  16. # c.arg_to_cmd = first positional argument to the primary command
  17. # c.arg_to_cmd2 = first positional argument to the secondary command
  18. #
  19. # c.option(option_string, [bool argument_required]) = test for option with optional positional argument to option test
  20. # c.option_with_arg(option_string) = test for option and mandatory positional argument to option
  21. # c.flag(flag_string) = test for presence of a "option=argument" style flag
  22. #
  23. # c.arg(arg_string) = returns the next positional argument to the arg_string argument
  24. # c.flag_arg(flag_string) = returns the flag assignment for a "--option=argument" style flag
  25. #------------------------------------------------------------------------------------
  26. # Application start
  27. def main():
  28. import sys
  29. from Naked.commandline import Command
  30. from Naked.toolshed.state import StateObject
  31. #------------------------------------------------------------------------------------------
  32. # [ Instantiate command line object ]
  33. # used for all subsequent conditional logic in the CLI application
  34. #------------------------------------------------------------------------------------------
  35. c = Command(sys.argv[0], sys.argv[1:])
  36. #------------------------------------------------------------------------------
  37. # [ Instantiate state object ]
  38. #------------------------------------------------------------------------------
  39. state = StateObject()
  40. #------------------------------------------------------------------------------------------
  41. # [ Command Suite Validation ] - early validation of appropriate command syntax
  42. # Test that user entered at least one argument to the executable, print usage if not
  43. #------------------------------------------------------------------------------------------
  44. if not c.command_suite_validates():
  45. from {{app_name}}.settings import usage as {{app_name}}_usage
  46. print({{app_name}}_usage)
  47. sys.exit(1)
  48. #------------------------------------------------------------------------------------------
  49. # [ NAKED FRAMEWORK COMMANDS ]
  50. # Naked framework provides default help, usage, and version commands for all applications
  51. # --> settings for user messages are assigned in the lib/{{app_name}}/settings.py file
  52. #------------------------------------------------------------------------------------------
  53. if c.help(): # User requested {{app_name}} help information
  54. from {{app_name}}.settings import help as {{app_name}}_help
  55. print({{app_name}}_help)
  56. sys.exit(0)
  57. elif c.usage(): # User requested {{app_name}} usage information
  58. from {{app_name}}.settings import usage as {{app_name}}_usage
  59. print({{app_name}}_usage)
  60. sys.exit(0)
  61. elif c.version(): # User requested {{app_name}} version information
  62. from {{app_name}}.settings import app_name, major_version, minor_version, patch_version
  63. version_display_string = app_name + ' ' + major_version + '.' + minor_version + '.' + patch_version
  64. print(version_display_string)
  65. sys.exit(0)
  66. #------------------------------------------------------------------------------------------
  67. # [ PRIMARY COMMAND LOGIC ]
  68. # Enter your command line parsing logic below
  69. #------------------------------------------------------------------------------------------
  70. # [[ Example usage ]] ------------------------------->>>
  71. # if c.cmd == 'hello':
  72. # if c.cmd2 = 'world':
  73. # if c.option('--print'):
  74. # print('Hello World!')
  75. # elif c.cmd == 'spam':
  76. # if c.option_with_arg('--with'):
  77. # friend_of_spam = c.arg('--with') # user enters {{app_name}} spam --with eggs
  78. # print('spam and ' + friend_of_spam) # prints 'spam and eggs'
  79. # elif c.cmd == 'naked':
  80. # if c.flag("--language"):
  81. # lang = c.flag_arg("--language") # user enters {{app_name}} naked --language=python
  82. # print("Naked & " + lang) # prints 'Naked & python'
  83. # End example --------------------------------------->>>
  84. #------------------------------------------------------------------------------------------
  85. # [ DEFAULT MESSAGE FOR MATCH FAILURE ]
  86. # Message to provide to the user when all above conditional logic fails to meet a true condition
  87. #------------------------------------------------------------------------------------------
  88. else:
  89. print("Could not complete the command that you entered. Please try again.")
  90. sys.exit(1) #exit
  91. if __name__ == '__main__':
  92. main()
  93. """