defaults.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // mauflag - An extendable command-line argument parser for Golang
  2. // Copyright (C) 2016 Maunium
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. // You should have received a copy of the GNU General Public License
  13. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. package mauflag
  15. import (
  16. "os"
  17. )
  18. var defaultSet = New(os.Args[1:])
  19. // DefaultSet returns the default flagset which takes its arguments from os.Args
  20. func DefaultSet() *Set {
  21. return defaultSet
  22. }
  23. // Make creates and registers a flag
  24. func Make() *Flag {
  25. return DefaultSet().Make()
  26. }
  27. // MakeKey creates and registers a flag with the given short and long keys
  28. func MakeKey(short, long string) *Flag {
  29. return DefaultSet().MakeKey(short, long)
  30. }
  31. // MakeFull creates and registers a flag with the given short and long keys, usage string and default value
  32. func MakeFull(short, long, usage, defVal string) *Flag {
  33. return DefaultSet().MakeFull(short, long, usage, defVal)
  34. }
  35. // Parse the command line arguments into mauflag form
  36. func Parse() error {
  37. return DefaultSet().Parse()
  38. }
  39. // Args returns the arguments that weren't associated with any flag
  40. func Args() []string {
  41. return DefaultSet().Args()
  42. }
  43. // Arg returns the string at the given index from the list Args() returns
  44. // If the index does not exist, Arg will return an empty string.
  45. func Arg(i int) string {
  46. return DefaultSet().Arg(i)
  47. }
  48. // NArg returns the number of arguments not associated with any flags
  49. func NArg() int {
  50. return len(DefaultSet().args)
  51. }
  52. // MakeHelpFlag creates the -h, --help flag
  53. func MakeHelpFlag() (*bool, *Flag) {
  54. return DefaultSet().MakeHelpFlag()
  55. }
  56. // CheckHelpFlag checks if the help flag is set and prints the help page if needed.
  57. // Return value tells whether or not the help page was printed
  58. func CheckHelpFlag() bool {
  59. return DefaultSet().CheckHelpFlag()
  60. }
  61. // SetHelpTitles sets the first line (program name and basic explanation) and basic usage specification
  62. func SetHelpTitles(firstLine, basicUsage string) {
  63. DefaultSet().SetHelpTitles(firstLine, basicUsage)
  64. }
  65. // PrintHelp prints the help page
  66. func PrintHelp() {
  67. DefaultSet().PrintHelp()
  68. }