Advertisement
liorean

params.cmd

Feb 6th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 4.37 KB | None | 0 0
  1. @echo off
  2. setlocal enableextensions enabledelayedexpansion
  3. title Parameter parsing
  4. rem Handle database as UTF-8 instead of some system setting dependent ANSI code page.
  5. for /f "tokens=2 delims=:." %%x in ('chcp') do set cp=%%x
  6. chcp 65001 > nul
  7.  
  8.  
  9.  
  10. rem
  11. rem Parse parameters, handle arguments
  12. rem
  13. set "raw="
  14. set "flag="
  15. set "opt="
  16. set opt_def=default_value
  17. set "man="
  18.  
  19. rem No arguments given is an error
  20. if [%1] == [] goto :too_few_args
  21.  
  22. rem Otherwise enter arguments parsing loop
  23. :parse_arg
  24. set arg=%1
  25. set lead=!arg:~0,1!
  26. for %%x in (- /) do if [%%x] == [!lead!] goto :param
  27.  
  28. rem Argument didn't have a parameter decorator, so treat as raw argument.
  29. :raw_arg
  30. if defined raw goto :too_many_raw_args
  31. if not defined raw set raw=!arg!
  32. rem If raw argument is a file for us to open, check if it exists.
  33. if not exist !raw! goto :file_not_found
  34. goto :next
  35.  
  36. rem Argument had parameter decorator
  37. :param
  38. set param=!arg:~1!
  39. for %%x in (? h help) do if [%%x] == [!param!] goto :help
  40. for %%x in (f flag) do if [%%x] == [!param!] (
  41.     set flag=y
  42.     goto :next
  43. )
  44. for %%x in (F notflag) do if [%%x] == [!param!] (
  45.     set "flag="
  46.     goto :next
  47. )
  48. for %%x in (o opt) do if [%%x] == [!param!] (
  49.     set val_param=opt
  50.     goto :value
  51. )
  52. for %%x in (d opt_def) do if [%%x] == [!param!] (
  53.     set val_param=opt_def
  54.     goto :value
  55. )
  56. for %%x in (m man) do if [%%x] == [!param!] (
  57.     set val_param=man
  58.     goto :value
  59. )
  60. rem Maybe argument was not a parameter but a file that has a leading hyphen?
  61. if defined raw goto :invalid_param
  62. if not exist !arg! goto :invalid_param
  63. set raw=!arg!
  64. goto :next
  65.  
  66.  
  67. rem If parameter takes a value send to specific handler
  68. :value
  69. shift /1
  70. if [%1]==[] goto :missing_value
  71. set value=%1
  72. goto :value_!val_param!
  73.  
  74. :value_opt
  75. rem Test allowed values here
  76. set opt=!value!
  77. goto :next
  78.  
  79. :value_opt_def
  80. rem Test allowed values here
  81. set opt_def=!value!
  82. goto :next
  83.  
  84. :value_man
  85. rem Test allowed values here
  86. set man=!value!
  87. goto :next
  88.  
  89. rem Advance to next argument if one exists
  90. :next
  91. shift /1
  92. if [%1]==[] goto :final
  93. goto :parse_arg
  94.  
  95. rem Final steps of the arguments parser
  96. :final
  97. if not defined man goto :man_missing
  98. goto :program
  99.  
  100.  
  101.  
  102. rem
  103. rem Program body
  104. rem
  105. :program
  106. echo:%0
  107. echo:raw argument:   %raw%
  108. echo:flag parameter:    %flag%
  109. echo:optional value parameter:  %opt%
  110. echo:optional default value parameter:  %opt_def%
  111. echo:mandatory value parameter: %man%
  112. goto :end
  113.  
  114.  
  115.  
  116. rem
  117. rem Print help text
  118. rem
  119. :help
  120. echo:
  121. echo:Command:
  122. echo:   {command}[.cmd] [^<parameters^>] ^<filename^> [^<parameters^>]
  123. echo:
  124. echo:
  125. echo:Parameters:
  126. echo:   /f
  127. echo:   /flag
  128. echo:       Flag type parameter (enable, on, yes ...)
  129. echo:
  130. echo:   /F
  131. echo:   /noflag
  132. echo:       Flag type parameter (disable, off, no ...)
  133. echo:
  134. echo:   /o ^<optvalue^>
  135. echo:   /opt ^<optvalue^>
  136. echo:       Optional value parameter
  137. echo:
  138. echo:   /d ^<defvalue^>
  139. echo:   /opt_def ^<defvalue^>
  140. echo:       Optional value parameter with a default value
  141. echo:       Defaults to default_value
  142. echo:
  143. echo:   /m ^<manvalue^>
  144. echo:   /man ^<manvalue^>
  145. echo:       Mandatory value parameter
  146. echo:
  147. echo:   /?
  148. echo:   /h
  149. echo:   /help
  150. echo:       Prints this help text
  151. echo:
  152. echo:
  153. echo:Values:
  154. echo:   ^<optvalue^>
  155. echo:       Description of the allowed optional values.
  156. echo:
  157. echo:   ^<defvalue^>
  158. echo:       Description of the allowed optional values with default.
  159. echo:
  160. echo:   ^<manvalue^>
  161. echo:       Description of the allowed mandatory values.
  162. echo:
  163. goto :end
  164.  
  165.  
  166.  
  167. rem
  168. rem Error handling
  169. rem
  170. :too_few_args
  171. echo:--------
  172. echo:ERROR!
  173. echo:   {command} requires at least one argument.
  174. echo:   No argument was specified.
  175. echo:--------
  176. goto :help
  177.  
  178. :too_many_raw_args
  179. echo:--------
  180. echo:ERROR!
  181. echo:   {command} requires exactly one non-parameter argument.
  182. echo:   You have specified both «!raw!» and «!arg!».
  183. echo:--------
  184. goto :help
  185.  
  186. :file_not_found
  187. echo:--------
  188. echo:ERROR!
  189. echo:   {command} could not find file «!raw!».
  190. echo:--------
  191. goto :help
  192.  
  193. :invalid_param
  194. echo:--------
  195. echo:ERROR!
  196. echo:   {command} does not recognise the parameter «!arg!».
  197. echo:--------
  198. goto :help
  199.  
  200. :missing_value
  201. echo:--------
  202. echo:ERROR!
  203. echo:   {command} «!arg!» requires an additional argument value.
  204. echo:--------
  205. goto :help
  206.  
  207. :man_missing
  208. echo:--------
  209. echo:ERROR!
  210. echo:   {command} requires mandatory parameter «man».
  211. echo:--------
  212. goto :help
  213.  
  214.  
  215.  
  216. rem
  217. rem Restore code page and kill local variables
  218. rem
  219. :end
  220. chcp %cp% > nul
  221. endlocal
  222. goto :EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement