Documentation ¶
Overview ¶
Package cliflagutils provides a generic command line argument processing capability for XML-oriented programs. It provides a large set of useful arguments that can be individually (or as a group) enabled or disabled (i.e. hidden).
The package imports github.com/fbaube/(fileutils,stringutils,wasmutils)
Index ¶
- Variables
- func CheckMustUsage() error
- func DisableAllFlags()
- func DisableFlags(s string)
- func EnableAllFlags()
- func EnableFlags(s string)
- func EnvarNameFromFlagName(s string) string
- func FlagNameFromEnvarName(s string) string
- func HasFlag(haystack []string, needle string) bool
- func MyUsage()
- func ParseEnvars() error
- type GCtx
- type XmlAppConfiguration
Constants ¶
This section is empty.
Variables ¶
var UMM = map[string]string{
"h": "Show extended help message and exit",
"g": "Group all generated files in same-named folder \n" +
"(e.g. ./Filenam.xml maps to ./Filenam.xml_gxml/Filenam.*)",
"m": "Import input file(s) to database",
"p": "Pretty-print to file with \"fmtd-\" prepended to file extension",
"t": "gTree written to file with \"gtr-\" prepended to file extension",
"k": "gTokens written to file with \"gtk-\" prepended to file extension",
"v": "Validate input file(s) (using xmllint) (with flag \"-c\" or \"-s\")",
"z": "Zero out the database",
"D": "Turn on debugging",
"L": "Follow symbolic links in directory recursion",
"c": "XML `catalog_filepath` (do not use with \"-s\" flag)",
"o": "`output_filepath` (possibly ignored, depending on command)",
"d": "Database mmmc.db `directory_path`",
"r": "Run REST server on `port_number`",
"s": "DTD schema file(s) `directory_path` (.dtd, .mod)",
}
UMM is the Usage Message Map. We do this to provide (here) a quick option reference, and (below) to make the actual code clearer. e.g. commits := map[string]int { "rsc": 3711, "r": 2138, }
Functions ¶
func CheckMustUsage ¶
func CheckMustUsage() error
CheckMustUsage returns a non-nil loggable error message if the caller should abort execution.
func DisableAllFlags ¶
func DisableAllFlags()
func DisableFlags ¶
func DisableFlags(s string)
func EnableAllFlags ¶
func EnableAllFlags()
func EnableFlags ¶
func EnableFlags(s string)
func EnvarNameFromFlagName ¶
func FlagNameFromEnvarName ¶
func MyUsage ¶
func MyUsage()
MyUsage displays (1) the app name (or "(wasm)"), plus (2) a usage summary (see the func body), plus (3) the flags' usage message. TODO: Should not return info for flags that are Hidden (i.e. disabled).
func ParseEnvars ¶
func ParseEnvars() error
ParseEnvars is based on https://scene-si.org/2020/04/28/extending-pflag-with-environment-variables/
Types ¶
type GCtx ¶
type GCtx struct { // Context, used mainly for cancellation context.Context // = context.Context.TODO // Database connection *sqlx.DB // Database transaction *sqlx.Tx // XML configuration *XmlAppConfiguration // OwnLogPfx is own log prefix (should include a blank if needed) OwnLogPfx string }
GCtx carries background info thru a thread or goroutine. Golang best practices for `Context` say that 1) It should not be embedded anywhere, but rather should be passed as the first argument to func calls, and 2) It should contain only request-scoped data. But we won't abide by those rules for Context because 1) We only use it for cancellation, and 2) We want zero-argument method calls, and 3) We also store data that is NOT request-scoped.
type XmlAppConfiguration ¶
type XmlAppConfiguration struct { AppName string DBhandle *DU.MmmcDB Infile, Outfile, Dbdir, Xmlcatfile, Xmlschemasdir FU.PathProps // NOT ptr! Barfs at startup. RestPort int // CLI flags FollowSymLinks, Pritt, DBdoImport, Help, Debug, GroupGenerated, GTokens, GTree, Validate, DBdoZeroOut bool // Result of processing CLI arg for input file(s) SingleFile bool // Result of processing CLI args (-c, -s) *XU.XmlCatalogFile PrittOutput io.Writer // contains filtered or unexported fields }
XmlAppConfiguration can probably be used with various 3rd-party utilities.
func GetXmlAppConfiguration ¶
func GetXmlAppConfiguration() *XmlAppConfiguration
func NewXmlAppConfiguration ¶
func NewXmlAppConfiguration(osArgs []string) (*XmlAppConfiguration, error)
NewXmlAppConfiguration processes CLI arguments for any XML-related command. It takes the CLI arguments as calling parameters, rather than accessing them directly itself, to facilitate testing, and enable running in-browser as wasm.