Crates.io | aargvark_proc_macros |
lib.rs | aargvark_proc_macros |
version | 3.2.5 |
source | src |
created_at | 2023-06-11 18:05:18.678483 |
updated_at | 2024-11-09 18:42:25.512877 |
description | Helper crate for aargvark |
homepage | https://github.com/andrewbaxter/aargvark |
repository | https://github.com/andrewbaxter/aargvark |
max_upload_size | |
id | 887508 |
size | 35,203 |
A simple and consistent derive-based command line argument parsing, in the same genre as Clap-derive. It currently supports
Generally speaking this is intended to make CLI parsing simple by virtue of being simple and consistent, rather than poweruser-optimized keypress-minimizing parsing.
This attempts to support parsing arbitrarily complex command line arguments. Like with Serde, you can combine structs, vecs, enums in any way you want. Just because you can doesn't mean you should.
$ ; This is an example help output, sans light ansi styling
$ spagh -h
Usage: spagh COMMAND [ ...FLAGS]
A small CLI for querying, publishing, and administrating spaghettinuum.
COMMAND: COMMAND
[--debug]
COMMAND: ping | get | http | ssh | identity | publish | admin
ping ... Simple liveness check
get ... Request values associated with provided identity and keys
from a resolver
http ...
ssh ...
identity ... Commands for managing identities
publish ... Commands for publishing data
admin ... Commands for node administration
$ spagh publish set -h
Usage: spagh publish set IDENTITY DATA
IDENTITY: IDENTITY-SECRET-ARG Identity to publish as
DATA: <PATH> | - Data to publish. Must be json in the
structure `{KEY: {"ttl": MINUTES, "value":
DATA}, ...}`. `KEY` is a string that's a
dotted list of key segments, with `/` to
escape dots and escape characters.
IDENTITY-SECRET-ARG: local
An identity with its associated secret.
local <PATH> A file containing a generated key
Why this and not Clap?
#[derive(Aargvark)]
on any enum/structure)Why not this?
-v
-vv
-vvv
) break patterns and probably won't ever be implemented. (Other things just haven't been implemented yet due to lack of time)To add it to your project, run
cargo add aargvark
To parse command line arguments
Define the data type you want to parse them into, like
/// General description for the command.
#[derive(Aargvark)]
struct MyArgs {
/// Field documentation.
velociraptor: String,
#[vark(flag = "-d", flag = "--deadly")]
deadly: bool,
color_pattern: Option<ColorPattern>,
}
Vark it
let args = vark::<MyArgs>();
Non-optional fields become positional arguments unless you give them a flag with #[vark(flag = "--flag")]
. Optional fields become optional (--long
) arguments. If you want a bool
flag that's enabled if the flag is specified (i.e. doesn't take a value), use Option<()>
.
You can derive structs, enums, and tuples, and there are implementations for Vec
, HashSet
, Map
with FromString
keys and values as K=V
arguments, most Ip
and SocketAddr
types, and PathBuf
built in.
Some additional wrappers are provided for automatically loading (and parsing) files:
AargvarkFile<T>
AargvarkJson<T>
requires feature serde_json
AargvarkYaml<T>
requires feature serde_yaml
To parse your own types, implement AargvarkTrait
, or if your type takes a single string argument you can implement AargvarkFromStr
which is slightly simpler.
Sequences, plural fields, Vecs
Sequence elements are space separated. The way sequence parsing works is it attempts to parse as many elements as possible. When parsing one element fails, it rewinds to after it parsed the last successful element and proceeds from the next field after the sequence.
Use flags, replace flags, and add additional flags
Add ex: #[vark(flag="--target-machine", flag="-tm")]
to a field.
If the field was optional, this will replace the default flag. If the field was non-optional, this will make it require a flag instead of being positional.
Rename enum variant keys
Add ex: #[vark(name="my-variant")]
to a variant.
This changes the command line key used to select a variant.
Prevent recursion in help
Add #[vark(break_help)]
to a type, field, or variant to prevent recursing into any of the children when displaying help. This is useful for subcommand enums - attach this to the enum and it will list the variants but not the variants' arguments (unless you do -h
after specifying one on the command line).
Change the help placeholder string
Add #[vark(placeholder="TARGET-MACHINE")]
to a type, field, or variant.
This is the capitalized text (like XYZ) after an option that basically means "see XYZ section for more details"