I created a Go library nzargv that arranges command line arguments nicely.

zakuro9715/nzargv PkgGoDevGitHubRepostars

Overview

-ab=0 --value1=0 --value2 1 This kind of thing -a -b=0 --value=0 --value2=1 Will do this

What to do

(The number of values must be specified in advance with FlagN --Flags like -a --Priced flags like -a = b --Unequal priced flags, such as -a b --Flags that group single characters, such as -abc (expanded as -a -b -c) --A flag with a value above, such as -abc = d. Only the last flag (c in this case) is set to a value --A version without the above equals, such as -abc d --Treats values that do not start with - and are not flag values as Arg --Comma separated values are recognized as multiple values. That is, if FlagN of a is 2, then --a = b, c and --a b c are the same. --Even if a flag with the same name is specified multiple times, it remains the same, but it can be merged with a method

Slightly subtle behavior

--FlagN specifies the number of values, which is currently ignored for comma separated values.

How to use

Create an App with New, call the setting method, and then call the normalize method.

ToStrings is attached to the method whose return value is string slice. If it is not attached, [] nzargv.Value is returned. Since Value has Name and Value, you can retrieve the value of the flag.

For more information, see GitHub or go.dev.

Development motive

Useful libraries for creating command line apps, such as spf13 / cobra and urafave / cli There are many.

However, these libraries are just for creating CLI apps, and they often feel over-functional, making them difficult to use when you want to create a slightly special application.

However, handling command line arguments is more cumbersome than you might think, and it's unproductive to write this every time.

So, when processing command line arguments, I created a library that normalizes in a form that is easy to handle in advance.

Recommended Posts

I created a Go library nzargv that arranges command line arguments nicely.
I made a LINE BOT that returns parrots with Go
I created a Python library to call the LINE WORKS API
I made a library to separate Japanese sentences nicely
Created a Python library DateTimeRange that handles time ranges
[Python] I tried to make a simple program that works on the command line using argparse.
[Python] I made a Line bot that randomly asks English words.
Created a library for python that can easily handle morpheme division
I made a lo command that is more useful than ls
I made a library that adds docstring to a Python stub file.