add support for visibility in arg! macro

This commit is contained in:
nora 2021-12-15 22:07:54 +01:00
parent 6054c988a0
commit 0619bdf16b
3 changed files with 45 additions and 24 deletions

View file

@ -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"

View file

@ -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),
}

View file

@ -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,7 +61,8 @@ 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.
/// ```