mirror of
https://github.com/Noratrieb/badargs.git
synced 2026-01-14 11:55:00 +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]
|
||||
name = "badargs"
|
||||
version = "0.1.2"
|
||||
version = "0.2.0"
|
||||
edition = "2018"
|
||||
license = "MIT"
|
||||
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
|
||||
//! ```
|
||||
//! # use badargs::arg;
|
||||
//! use badargs::arg;
|
||||
//!
|
||||
//! arg!(Force: "force", 'f' -> bool);
|
||||
//! 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 tuples manually
|
||||
//! The recommended way to use `badargs` is by invoking the macro [`badargs!`]
|
||||
//! ```
|
||||
//! # use badargs::arg;
|
||||
//! # arg!(Force: "force", 'f' -> bool);
|
||||
//! # arg!(OutFile: "output", 'o' -> String);
|
||||
//! use badargs::arg;
|
||||
//!
|
||||
//! arg!(Force: "force", 'f' -> bool);
|
||||
//! arg!(OutFile: "output", 'o' -> String);
|
||||
//!
|
||||
//! 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;
|
||||
//! # arg!(Force: "force", 'f' -> bool);
|
||||
//! # arg!(OutFile: "output", 'o' -> String);
|
||||
//! # let args = badargs::badargs!(Force, OutFile);
|
||||
//! use badargs::arg;
|
||||
//! arg!(Force: "force", 'f' -> bool);
|
||||
//! arg!(OutFile: "output", 'o' -> String);
|
||||
//!
|
||||
//! let args = badargs::badargs!(Force, OutFile);
|
||||
//!
|
||||
//! let force: Option<&bool> = args.get::<Force>();
|
||||
//! let out_file: Option<&String> = args.get::<OutFile>();
|
||||
//! let out_file = args.get::<OutFile>();
|
||||
//! ```
|
||||
|
||||
mod macros;
|
||||
|
|
@ -138,7 +143,9 @@ mod error {
|
|||
/// Invalid schema
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub enum SchemaError {
|
||||
/// The argument name was already provided for a different argument
|
||||
NameAlreadyExists(String),
|
||||
/// Currently not used
|
||||
InvalidSchema(String),
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,25 @@
|
|||
///
|
||||
/// 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);
|
||||
/// ```
|
||||
/// is a shorthand for
|
||||
/// ```
|
||||
/// # use badargs::{arg, CliArg};
|
||||
/// use badargs::{arg, CliArg};
|
||||
///
|
||||
/// struct Force;
|
||||
///
|
||||
/// impl CliArg for Force {
|
||||
|
|
@ -23,15 +36,15 @@
|
|||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! arg {
|
||||
($name:ident: $long:literal, $short:literal -> $result:ty) => {
|
||||
arg!(@$name: ($long, ::std::option::Option::Some($short)) -> $result);
|
||||
($vis:vis $name:ident: $long:literal, $short:literal -> $result:ty) => {
|
||||
arg!(@$vis $name: ($long, ::std::option::Option::Some($short)) -> $result);
|
||||
};
|
||||
($name:ident: $long:literal -> $result:ty) => {
|
||||
arg!(@$name: ($long, ::std::option::Option::None) -> $result);
|
||||
($vis:vis $name:ident: $long:literal -> $result:ty) => {
|
||||
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)]
|
||||
struct $name;
|
||||
$vis struct $name;
|
||||
|
||||
impl $crate::CliArg for $name {
|
||||
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
|
||||
/// nested tuples for you, since that's what's internally used.
|
||||
/// ```
|
||||
/// # use badargs::arg;
|
||||
/// # arg!(Force: "force", 'f' -> bool);
|
||||
/// # arg!(OutFile: "outfile", 't' -> bool);
|
||||
/// # arg!(SetUpstream: "set-upstream", 'x' -> bool);
|
||||
/// # fn main() {
|
||||
/// let args = badargs::badargs!(Force, OutFile, SetUpstream);
|
||||
/// # }
|
||||
/// use badargs::arg;
|
||||
/// arg!(Force: "force", 'f' -> bool);
|
||||
/// arg!(OutFile: "outfile", 't' -> bool);
|
||||
/// arg!(SetUpstream: "set-upstream", 'x' -> bool);
|
||||
///
|
||||
/// fn main() {
|
||||
/// let args = badargs::badargs!(Force, OutFile, SetUpstream);
|
||||
/// }
|
||||
/// ```
|
||||
/// will be expanded into
|
||||
/// ```
|
||||
/// # use badargs::arg;
|
||||
/// # arg!(Force: "force", 'f' -> bool);
|
||||
/// # arg!(OutFile: "outfile", 't' -> bool);
|
||||
/// # arg!(SetUpstream: "set-upstream", 'x' -> bool);
|
||||
/// let args = badargs::badargs::<(Force, (OutFile, SetUpstream))>();
|
||||
/// use badargs::arg;
|
||||
/// arg!(Force: "force", 'f' -> bool);
|
||||
/// arg!(OutFile: "outfile", 't' -> bool);
|
||||
/// arg!(SetUpstream: "set-upstream", 'x' -> bool);
|
||||
///
|
||||
/// fn main() {
|
||||
/// let args = badargs::badargs::<(Force, (OutFile, SetUpstream))>();
|
||||
/// }
|
||||
/// ```
|
||||
/// This only provides a minor benefit for programs with a small amount of args, but is
|
||||
/// 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
|
||||
{
|
||||
|
||||
$crate::badargs::<($crate::badargs!(@inner $($tail),+))>()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue