getflags 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. .TH GETFLAGS 8
  2. .SH NAME
  3. getflags, usage \- command-line parsing for shell scripts
  4. .SH SYNOPSIS
  5. .B aux/getflags $*
  6. .PP
  7. .B aux/usage
  8. .SH DESCRIPTION
  9. .I Getflags
  10. parses the options in its command-line arguments
  11. according to the environment variable
  12. .BR $flagfmt .
  13. This variable should be a list of comma-separated options.
  14. Each option can be a single letter, indicating that it does
  15. not take arguments, or a letter followed by the space-separated
  16. names of its arguments.
  17. .I Getflags
  18. prints an
  19. .IR rc (1)
  20. script on standard output which initializes the
  21. environment variable
  22. .BI $flag x
  23. for every option mentioned in
  24. .BR $flagfmt .
  25. If the option is not present on the command-line, the script
  26. sets that option's flag variable to an empty list.
  27. Otherwise, the script sets that option's flag variable with
  28. a list containing the option's arguments or,
  29. if the option takes no arguments,
  30. with the string
  31. .BR 1 .
  32. The script also sets the variable
  33. .B $*
  34. to the list of arguments following the options.
  35. The final line in the script sets the
  36. .B $status
  37. variable, to the empty string on success
  38. and to the string
  39. .B usage
  40. when there is an error parsing the command line.
  41. .PP
  42. .I Usage
  43. prints a usage message to standard error.
  44. It creates the message using
  45. .BR $flagfmt ,
  46. as described above,
  47. .BR $args ,
  48. which should contain the string to be printed explaining
  49. non-option arguments,
  50. and
  51. .BR $0 ,
  52. the program name
  53. (see
  54. .IR rc (1)).
  55. .SH EXAMPLE
  56. Parse the arguments for
  57. .IR leak (1):
  58. .IP
  59. .EX
  60. flagfmt='b,s,f binary,r res,x width'
  61. args='name | pid list'
  62. if(! ifs=() eval `{aux/getflags $*} || ~ $#* 0){
  63. aux/usage
  64. exit usage
  65. }
  66. .EE
  67. .SH SOURCE
  68. .B /sys/src/cmd/aux/getflags.c
  69. .br
  70. .B /sys/src/cmd/aux/usage.c
  71. .SH SEE ALSO
  72. .IR arg (2)