diff --git a/Cargo.toml b/Cargo.toml index 43f61da..3a59b88 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "badargs" -version = "0.2.0" +version = "0.1.2" edition = "2018" license = "MIT" description = "Type safe zero-dependency argument parser" diff --git a/src/lib.rs b/src/lib.rs index bd7f8a6..7cf9502 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,34 +5,29 @@ //! //! 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. //! -//! The recommended way to use `badargs` is by invoking the macro [`badargs!`] +//! You can also use the [`badargs!`] macro if you have many arguments and don't want to nest +//! the tuples manually //! ``` -//! 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 also invoke the [`badargs()`] function directly -//! -//! Getting the values is done using the [`BadArgs::get`] function +//! You can then get values using your declared arguments //! ``` -//! 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::(); -//! let out_file = args.get::(); +//! let out_file: Option<&String> = args.get::(); //! ``` mod macros; @@ -143,9 +138,7 @@ 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), } diff --git a/src/macros.rs b/src/macros.rs index b708e44..746d98a 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,25 +1,12 @@ /// /// Declare your arguments using this macro. -/// -/// Possible patterns: /// ``` -/// use badargs::arg; -/// -/// arg!(LongOrShort: "long-or-short", 's' -> bool); -/// arg!(OnlyLong: "only-long" -> bool); -/// arg!(pub OtherModule: "other-module" -> bool); -/// ``` -/// -/// -/// ``` -/// use badargs::arg; -/// +/// # 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 { @@ -36,15 +23,15 @@ /// ``` #[macro_export] macro_rules! arg { - ($vis:vis $name:ident: $long:literal, $short:literal -> $result:ty) => { - arg!(@$vis $name: ($long, ::std::option::Option::Some($short)) -> $result); + ($name:ident: $long:literal, $short:literal -> $result:ty) => { + arg!(@$name: ($long, ::std::option::Option::Some($short)) -> $result); }; - ($vis:vis $name:ident: $long:literal -> $result:ty) => { - arg!(@$vis $name: ($long, ::std::option::Option::None) -> $result); + ($name:ident: $long:literal -> $result:ty) => { + arg!(@$name: ($long, ::std::option::Option::None) -> $result); }; - (@$vis:vis $name:ident: ($long:literal, $short:expr) -> $result:ty) => { + (@$name:ident: ($long:literal, $short:expr) -> $result:ty) => { #[derive(Default)] - $vis struct $name; + struct $name; impl $crate::CliArg for $name { type Content = $result; @@ -61,30 +48,25 @@ macro_rules! arg { } /// -/// A shorthand for calling the [`badargs`](crate::badargs()) main function -/// +/// A shorthand for calling the [`badargs::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); -/// -/// 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); +/// 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. @@ -100,6 +82,7 @@ macro_rules! badargs { { #[allow(unused_parens)] // allow this because there might only be one arg { + $crate::badargs::<($crate::badargs!(@inner $($tail),+))>() } }