| 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 | |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | package mauflag |
| 18 | |
| 19 | import ( |
| 20 | "os" |
| 21 | ) |
| 22 | |
| 23 | var defaultSet = New(os.Args[1:]) |
| 24 | |
| 25 | // DefaultSet returns the default flagset which takes its arguments from os.Args |
| 26 | func DefaultSet() *Set { |
| 27 | return defaultSet |
| 28 | } |
| 29 | |
| 30 | // Make creates and registers a flag |
| 31 | func Make() *Flag { |
| 32 | return DefaultSet().Make() |
| 33 | } |
| 34 | |
| 35 | // MakeKey creates and registers a flag with the given short and long keys |
| 36 | func MakeKey(short, long string) *Flag { |
| 37 | return DefaultSet().MakeKey(short, long) |
| 38 | } |
| 39 | |
| 40 | // MakeFull creates and registers a flag with the given short and long keys, usage string and default value |
| 41 | func MakeFull(short, long, usage, defVal string) *Flag { |
| 42 | return DefaultSet().MakeFull(short, long, usage, defVal) |
| 43 | } |
| 44 | |
| 45 | // Parse the command line arguments into mauflag form |
| 46 | func Parse() error { |
| 47 | return DefaultSet().Parse() |
| 48 | } |
| 49 | |
| 50 | // Args returns the arguments that weren't associated with any flag |
| 51 | func Args() []string { |
| 52 | return DefaultSet().Args() |
| 53 | } |
| 54 | |
| 55 | // Arg returns the string at the given index from the list Args() returns |
| 56 | // If the index does not exist, Arg will return an empty string. |
| 57 | func Arg(i int) string { |
| 58 | return DefaultSet().Arg(i) |
| 59 | } |
| 60 | |
| 61 | // NArg returns the number of arguments not associated with any flags |
| 62 | func NArg() int { |
| 63 | return len(DefaultSet().args) |
| 64 | } |
| 65 | |
| 66 | // MakeHelpFlag creates the -h, --help flag |
| 67 | func MakeHelpFlag() (*bool, *Flag) { |
| 68 | return DefaultSet().MakeHelpFlag() |
| 69 | } |
| 70 | |
| 71 | // CheckHelpFlag checks if the help flag is set and prints the help page if needed. |
| 72 | // Return value tells whether or not the help page was printed |
| 73 | func CheckHelpFlag() bool { |
| 74 | return DefaultSet().CheckHelpFlag() |
| 75 | } |
| 76 | |
| 77 | // SetHelpTitles sets the first line (program name and basic explanation) and basic usage specification |
| 78 | func SetHelpTitles(firstLine, basicUsage string) { |
| 79 | DefaultSet().SetHelpTitles(firstLine, basicUsage) |
| 80 | } |
| 81 | |
| 82 | // PrintHelp prints the help page |
| 83 | func PrintHelp() { |
| 84 | DefaultSet().PrintHelp() |
| 85 | } |