Cligen is a Nim library that simplifies command-line interface generation by inferring options directly from proc signatures. With just one line of code, create intuitive CLIs without complicated specifications or parsing APIs. Ideal for rapid development, this tool streamlines command handling while maintaining clarity and efficiency.
cligen is a powerful Nim library designed to simplify the creation of command-line interfaces (CLIs) by inferring and generating options and argument parsing directly from proc signatures. Inspired by Python’s argh and designed for ease of use, cligen leverages Nim’s reflection capabilities to create parser-dispatchers that translate command-line inputs into function calls effortlessly.
import cligen; dispatch fun
Usage:
fun [optional-params] [args: string...]
Options:
-h, --help print this cligen-erated help
-f=, --foo= int 1 set foo
-b=, --bar= float 2.0 set bar
--baz= string "x" set baz
-v, --verb bool false set verb
Here’s an example of a simple command-line function with cligen:
proc fun(foo=1, bar=2.0, baz="x", verb=false, args: seq[string]): int =
result = 1 # Placeholder for real logic
dispatch fun
cligen also supports subcommands similar to tools like git, allowing for complex command structures with ease:
proc foo(myRequired: int, mynums: seq[int], foo=1) = discard
proc bar(yippee: int, myFlts: seq[float], verb=false) = discard
dispatchMulti([foo], [bar])
Many parameters can be customized, including short option aliases and their help descriptions. Users can also override built-in parsers or provide their own parsing logic to meet specific requirements.
For more information, comprehensive documentation can be found through the following resources:
By utilizing cligen, developers can create robust command-line interfaces that are efficient, flexible, and easy to manage.
No comments yet.
Sign in to be the first to comment.