ex_wget.nim 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import argument_parser, tables, strutils, parseutils
  2. ## Example defining a subset of wget's functionality
  3. const
  4. PARAM_VERSION = @["-V", "--version"]
  5. PARAM_HELP = @["-h", "--help"]
  6. PARAM_BACKGROUND = @["-b", "--background"]
  7. PARAM_OUTPUT = @["-o", "--output"]
  8. PARAM_NO_CLOBBER = @["-nc", "--no-clobber"]
  9. PARAM_PROGRESS = @["--progress"]
  10. PARAM_NO_PROXY = @["--no-proxy"]
  11. template P(tnames: varargs[string], thelp: string, ttype = PK_EMPTY,
  12. tcallback: Tparameter_callback = nil) =
  13. ## Helper to avoid repetition of parameter adding boilerplate.
  14. params.add(new_parameter_specification(ttype, custom_validator = tcallback,
  15. help_text = thelp, names = tnames))
  16. template got(param: varargs[string]) =
  17. ## Just dump the detected options on output.
  18. if result.options.hasKey(param[0]): echo("Found option '$1'." % [param[0]])
  19. proc parse_progress(parameter: string; value: var Tparsed_parameter): string =
  20. ## Custom parser and validator of progress types for PARAM_PROGRESS.
  21. ##
  22. ## If the user specifies the PARAM_PROGRESS option this proc will be called
  23. ## so we can validate the input. The proc returns a non empty string if
  24. ## something went wrong with the description of the error, otherwise
  25. ## execution goes ahead.
  26. ##
  27. ## This validator only accepts values without changing the final output.
  28. if value.str_val == "bar" or value.str_val == "dot":
  29. return
  30. result = "The string $1 is not valid, use bar or dot." % [value.str_val]
  31. proc process_commandline(): Tcommandline_results =
  32. ## Parses the commandline.
  33. ##
  34. ## Returns a Tcommandline_results with at least two positional parameter,
  35. ## where the last parameter is implied to be the destination of the copying.
  36. var params: seq[Tparameter_specification] = @[]
  37. P(PARAM_VERSION, "Shows the version of the program")
  38. P(PARAM_HELP, "Shows this help on the commandline", PK_HELP)
  39. P(PARAM_BACKGROUND, "Continues execution in the background")
  40. P(PARAM_OUTPUT, "Specifies a specific output file name", PK_STRING)
  41. P(PARAM_NO_CLOBBER, "Skip downloads that would overwrite existing files")
  42. P(PARAM_PROGRESS, "Select progress look (bar or dot)",
  43. PK_STRING, parse_progress)
  44. P(PARAM_NO_PROXY, "Don't use proxies even if available")
  45. result = parse(params)
  46. if result.positional_parameters.len < 1:
  47. echo "Missing URL(s) to download"
  48. echo_help(params)
  49. quit()
  50. got(PARAM_NO_CLOBBER)
  51. got(PARAM_BACKGROUND)
  52. got(PARAM_NO_PROXY)
  53. if result.options.hasKey(PARAM_VERSION[0]):
  54. echo "Version 3.1415"
  55. quit()
  56. if result.options.hasKey(PARAM_OUTPUT[0]):
  57. if result.positional_parameters.len > 1:
  58. echo "Error: can't use $1 option with multiple URLs" % [PARAM_OUTPUT[0]]
  59. echo_help(params)
  60. quit()
  61. echo "Will download to $1" % [result.options[PARAM_OUTPUT[0]].str_val]
  62. if result.options.hasKey(PARAM_PROGRESS[0]):
  63. echo "Will use progress type $1" % [result.options[PARAM_PROGRESS[0]].str_val]
  64. when true:
  65. let args = process_commandline()
  66. for param in args.positional_parameters:
  67. echo "Downloading $1" % param.str_val