mirror of
https://github.com/Noratrieb/badargs.git
synced 2026-01-14 19:55:08 +01:00
Compare commits
2 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0619bdf16b | |||
| 6054c988a0 |
3 changed files with 61 additions and 37 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "badargs"
|
name = "badargs"
|
||||||
version = "0.1.2"
|
version = "0.2.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
description = "Type safe zero-dependency argument parser"
|
description = "Type safe zero-dependency argument parser"
|
||||||
|
|
|
||||||
35
src/lib.rs
35
src/lib.rs
|
|
@ -5,29 +5,34 @@
|
||||||
//!
|
//!
|
||||||
//! Declare your arguments with structs. You probably want to use the macro for that
|
//! Declare your arguments with structs. You probably want to use the macro for that
|
||||||
//! ```
|
//! ```
|
||||||
//! # use badargs::arg;
|
//! use badargs::arg;
|
||||||
|
//!
|
||||||
//! arg!(Force: "force", 'f' -> bool);
|
//! arg!(Force: "force", 'f' -> bool);
|
||||||
//! arg!(OutFile: "output", 'o' -> String);
|
//! arg!(OutFile: "output", 'o' -> String);
|
||||||
//! ```
|
//! ```
|
||||||
//! Then you call the [`badargs`] function with all of your declared arguments. You probably
|
|
||||||
//! want to use a macro for that too.
|
|
||||||
//!
|
//!
|
||||||
//! You can also use the [`badargs!`] macro if you have many arguments and don't want to nest
|
//! The recommended way to use `badargs` is by invoking the macro [`badargs!`]
|
||||||
//! the tuples manually
|
|
||||||
//! ```
|
//! ```
|
||||||
//! # use badargs::arg;
|
//! use badargs::arg;
|
||||||
//! # arg!(Force: "force", 'f' -> bool);
|
//!
|
||||||
//! # arg!(OutFile: "output", 'o' -> String);
|
//! arg!(Force: "force", 'f' -> bool);
|
||||||
|
//! arg!(OutFile: "output", 'o' -> String);
|
||||||
|
//!
|
||||||
//! let args = badargs::badargs!(Force, OutFile);
|
//! let args = badargs::badargs!(Force, OutFile);
|
||||||
//! ```
|
//! ```
|
||||||
//! You can then get values using your declared arguments
|
//!
|
||||||
|
//! You can also invoke the [`badargs()`] function directly
|
||||||
|
//!
|
||||||
|
//! Getting the values is done using the [`BadArgs::get`] function
|
||||||
//! ```
|
//! ```
|
||||||
//! # use badargs::arg;
|
//! use badargs::arg;
|
||||||
//! # arg!(Force: "force", 'f' -> bool);
|
//! arg!(Force: "force", 'f' -> bool);
|
||||||
//! # arg!(OutFile: "output", 'o' -> String);
|
//! arg!(OutFile: "output", 'o' -> String);
|
||||||
//! # let args = badargs::badargs!(Force, OutFile);
|
//!
|
||||||
|
//! let args = badargs::badargs!(Force, OutFile);
|
||||||
|
//!
|
||||||
//! let force: Option<&bool> = args.get::<Force>();
|
//! let force: Option<&bool> = args.get::<Force>();
|
||||||
//! let out_file: Option<&String> = args.get::<OutFile>();
|
//! let out_file = args.get::<OutFile>();
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
mod macros;
|
mod macros;
|
||||||
|
|
@ -138,7 +143,9 @@ mod error {
|
||||||
/// Invalid schema
|
/// Invalid schema
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
pub enum SchemaError {
|
pub enum SchemaError {
|
||||||
|
/// The argument name was already provided for a different argument
|
||||||
NameAlreadyExists(String),
|
NameAlreadyExists(String),
|
||||||
|
/// Currently not used
|
||||||
InvalidSchema(String),
|
InvalidSchema(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,25 @@
|
||||||
///
|
///
|
||||||
/// Declare your arguments using this macro.
|
/// Declare your arguments using this macro.
|
||||||
|
///
|
||||||
|
/// Possible patterns:
|
||||||
/// ```
|
/// ```
|
||||||
/// # use badargs::arg;
|
/// use badargs::arg;
|
||||||
|
///
|
||||||
|
/// arg!(LongOrShort: "long-or-short", 's' -> bool);
|
||||||
|
/// arg!(OnlyLong: "only-long" -> bool);
|
||||||
|
/// arg!(pub OtherModule: "other-module" -> bool);
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use badargs::arg;
|
||||||
|
///
|
||||||
/// arg!(Force: "force", 'f' -> bool);
|
/// arg!(Force: "force", 'f' -> bool);
|
||||||
/// ```
|
/// ```
|
||||||
/// is a shorthand for
|
/// is a shorthand for
|
||||||
/// ```
|
/// ```
|
||||||
/// # use badargs::{arg, CliArg};
|
/// use badargs::{arg, CliArg};
|
||||||
|
///
|
||||||
/// struct Force;
|
/// struct Force;
|
||||||
///
|
///
|
||||||
/// impl CliArg for Force {
|
/// impl CliArg for Force {
|
||||||
|
|
@ -23,15 +36,15 @@
|
||||||
/// ```
|
/// ```
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! arg {
|
macro_rules! arg {
|
||||||
($name:ident: $long:literal, $short:literal -> $result:ty) => {
|
($vis:vis $name:ident: $long:literal, $short:literal -> $result:ty) => {
|
||||||
arg!(@$name: ($long, ::std::option::Option::Some($short)) -> $result);
|
arg!(@$vis $name: ($long, ::std::option::Option::Some($short)) -> $result);
|
||||||
};
|
};
|
||||||
($name:ident: $long:literal -> $result:ty) => {
|
($vis:vis $name:ident: $long:literal -> $result:ty) => {
|
||||||
arg!(@$name: ($long, ::std::option::Option::None) -> $result);
|
arg!(@$vis $name: ($long, ::std::option::Option::None) -> $result);
|
||||||
};
|
};
|
||||||
(@$name:ident: ($long:literal, $short:expr) -> $result:ty) => {
|
(@$vis:vis $name:ident: ($long:literal, $short:expr) -> $result:ty) => {
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct $name;
|
$vis struct $name;
|
||||||
|
|
||||||
impl $crate::CliArg for $name {
|
impl $crate::CliArg for $name {
|
||||||
type Content = $result;
|
type Content = $result;
|
||||||
|
|
@ -48,25 +61,30 @@ macro_rules! arg {
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
/// A shorthand for calling the [`badargs::badargs`] main function
|
/// A shorthand for calling the [`badargs`](crate::badargs()) main function
|
||||||
|
///
|
||||||
/// This macro lets you specify your arguments in a flat list, and then converts them into
|
/// This macro lets you specify your arguments in a flat list, and then converts them into
|
||||||
/// nested tuples for you, since that's what's internally used.
|
/// nested tuples for you, since that's what's internally used.
|
||||||
/// ```
|
/// ```
|
||||||
/// # use badargs::arg;
|
/// use badargs::arg;
|
||||||
/// # arg!(Force: "force", 'f' -> bool);
|
/// arg!(Force: "force", 'f' -> bool);
|
||||||
/// # arg!(OutFile: "outfile", 't' -> bool);
|
/// arg!(OutFile: "outfile", 't' -> bool);
|
||||||
/// # arg!(SetUpstream: "set-upstream", 'x' -> bool);
|
/// arg!(SetUpstream: "set-upstream", 'x' -> bool);
|
||||||
/// # fn main() {
|
///
|
||||||
|
/// fn main() {
|
||||||
/// let args = badargs::badargs!(Force, OutFile, SetUpstream);
|
/// let args = badargs::badargs!(Force, OutFile, SetUpstream);
|
||||||
/// # }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
/// will be expanded into
|
/// will be expanded into
|
||||||
/// ```
|
/// ```
|
||||||
/// # use badargs::arg;
|
/// use badargs::arg;
|
||||||
/// # arg!(Force: "force", 'f' -> bool);
|
/// arg!(Force: "force", 'f' -> bool);
|
||||||
/// # arg!(OutFile: "outfile", 't' -> bool);
|
/// arg!(OutFile: "outfile", 't' -> bool);
|
||||||
/// # arg!(SetUpstream: "set-upstream", 'x' -> bool);
|
/// arg!(SetUpstream: "set-upstream", 'x' -> bool);
|
||||||
|
///
|
||||||
|
/// fn main() {
|
||||||
/// let args = badargs::badargs::<(Force, (OutFile, SetUpstream))>();
|
/// let args = badargs::badargs::<(Force, (OutFile, SetUpstream))>();
|
||||||
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
/// This only provides a minor benefit for programs with a small amount of args, but is
|
/// This only provides a minor benefit for programs with a small amount of args, but is
|
||||||
/// very useful for larger arg amounts.
|
/// very useful for larger arg amounts.
|
||||||
|
|
@ -82,7 +100,6 @@ macro_rules! badargs {
|
||||||
{
|
{
|
||||||
#[allow(unused_parens)] // allow this because there might only be one arg
|
#[allow(unused_parens)] // allow this because there might only be one arg
|
||||||
{
|
{
|
||||||
|
|
||||||
$crate::badargs::<($crate::badargs!(@inner $($tail),+))>()
|
$crate::badargs::<($crate::badargs!(@inner $($tail),+))>()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue