mirror of
https://github.com/Noratrieb/badargs.git
synced 2026-01-14 19:55:08 +01:00
making optionality a bool
This commit is contained in:
parent
346c8835cd
commit
a33b81c58e
5 changed files with 47 additions and 34 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
use badargs::arg;
|
use badargs::arg;
|
||||||
|
|
||||||
arg!(OutFile: "output", 'o' -> Option<String>);
|
arg!(OutFile: "output", 'o' -> String, required);
|
||||||
arg!(Force: "force", 'f' -> bool);
|
arg!(Force: "force", 'f' -> bool);
|
||||||
arg!(OLevel: "optimize" -> usize);
|
arg!(OLevel: "optimize" -> usize);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,9 @@ where
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use badargs::arg;
|
/// # use badargs::arg;
|
||||||
/// arg!(OutFile: "output", 'o' -> Option<String>);
|
/// arg!(Force: "force", 'f' -> bool);
|
||||||
|
///
|
||||||
|
/// arg!(OutFile: "output", 'o' -> String, required);
|
||||||
/// // OutFile now implements CliArg
|
/// // OutFile now implements CliArg
|
||||||
/// ```
|
/// ```
|
||||||
// This trait requires any because some dynamic typing is done in the background
|
// This trait requires any because some dynamic typing is done in the background
|
||||||
|
|
@ -40,6 +42,7 @@ pub trait CliArg: Any {
|
||||||
|
|
||||||
fn long() -> &'static str;
|
fn long() -> &'static str;
|
||||||
fn short() -> Option<char>;
|
fn short() -> Option<char>;
|
||||||
|
fn required() -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The struct containing parsed argument information
|
/// The struct containing parsed argument information
|
||||||
|
|
@ -79,7 +82,6 @@ macro_rules! impl_cli_return {
|
||||||
|
|
||||||
impl_cli_return!(
|
impl_cli_return!(
|
||||||
for String => String;
|
for String => String;
|
||||||
for Option<String> => OptionString;
|
|
||||||
for bool => Bool;
|
for bool => Bool;
|
||||||
for isize => INum;
|
for isize => INum;
|
||||||
for usize => UNum
|
for usize => UNum
|
||||||
|
|
@ -90,7 +92,7 @@ mod sealed {
|
||||||
macro_rules! impl_ {
|
macro_rules! impl_ {
|
||||||
($($name:ty),+) => {$(impl SealedCliReturnValue for $name{})+};
|
($($name:ty),+) => {$(impl SealedCliReturnValue for $name{})+};
|
||||||
}
|
}
|
||||||
impl_!(String, Option<String>, bool, usize, isize);
|
impl_!(String, bool, usize, isize);
|
||||||
}
|
}
|
||||||
|
|
||||||
mod error {
|
mod error {
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,20 @@
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! arg {
|
macro_rules! arg {
|
||||||
|
// implicit optional
|
||||||
($name:ident: $long:literal, $short:literal -> $result:ty) => {
|
($name:ident: $long:literal, $short:literal -> $result:ty) => {
|
||||||
arg!(@$name: ($long, ::std::option::Option::Some($short)) -> $result);
|
arg!(@$name: ($long, ::std::option::Option::Some($short)) -> $result, false);
|
||||||
};
|
};
|
||||||
($name:ident: $long:literal -> $result:ty) => {
|
($name:ident: $long:literal -> $result:ty) => {
|
||||||
arg!(@$name: ($long, ::std::option::Option::None) -> $result);
|
arg!(@$name: ($long, ::std::option::Option::None) -> $result, false);
|
||||||
};
|
};
|
||||||
(@$name:ident: ($long:literal, $short:expr) -> $result:ty) => {
|
// required
|
||||||
|
($name:ident: $long:literal, $short:literal -> $result:ty, required) => {
|
||||||
|
arg!(@$name: ($long, ::std::option::Option::Some($short)) -> $result, true);
|
||||||
|
};
|
||||||
|
($name:ident: $long:literal -> $result:ty, required) => {
|
||||||
|
arg!(@$name: ($long, ::std::option::Option::None) -> $result, true);
|
||||||
|
};
|
||||||
|
(@$name:ident: ($long:literal, $short:expr) -> $result:ty, $required:literal) => {
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct $name;
|
struct $name;
|
||||||
|
|
||||||
|
|
@ -20,6 +28,10 @@ macro_rules! arg {
|
||||||
fn short() -> Option<char> {
|
fn short() -> Option<char> {
|
||||||
$short
|
$short
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn required() -> bool {
|
||||||
|
$required
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
11
src/parse.rs
11
src/parse.rs
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::error::CallError;
|
use crate::error::CallError;
|
||||||
use crate::schema::{Schema, SchemaKind, SchemaKindType};
|
use crate::schema::{Schema, SchemaKind};
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::iter::Peekable;
|
use std::iter::Peekable;
|
||||||
|
|
@ -60,13 +60,8 @@ fn parse_shorts(
|
||||||
.short(flag)
|
.short(flag)
|
||||||
.ok_or_else(|| CallError::ShortFlagNotFound(flag))?;
|
.ok_or_else(|| CallError::ShortFlagNotFound(flag))?;
|
||||||
|
|
||||||
let inner_kind = match command.kind {
|
match command.kind {
|
||||||
SchemaKind::Required(inner) => inner,
|
SchemaKind::String => {
|
||||||
SchemaKind::Optional(inner) => inner,
|
|
||||||
};
|
|
||||||
|
|
||||||
match inner_kind {
|
|
||||||
SchemaKindType::String => {
|
|
||||||
let next = args
|
let next = args
|
||||||
.next()
|
.next()
|
||||||
.ok_or_else(|| CallError::ExpectedValue(command.long.to_string()))?;
|
.ok_or_else(|| CallError::ExpectedValue(command.long.to_string()))?;
|
||||||
|
|
|
||||||
|
|
@ -7,18 +7,12 @@ use super::Result;
|
||||||
use crate::{CliArg, CliReturnValue, SchemaError};
|
use crate::{CliArg, CliReturnValue, SchemaError};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
|
||||||
pub enum SchemaKind {
|
|
||||||
Required(SchemaKindType),
|
|
||||||
Optional(SchemaKindType),
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// The type of value the argument returns
|
/// The type of value the argument returns
|
||||||
///
|
///
|
||||||
/// This could *maybe* also be solved with trait objects but lets keep this for now
|
/// This could *maybe* also be solved with trait objects but lets keep this for now
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
pub enum SchemaKindType {
|
pub enum SchemaKind {
|
||||||
String,
|
String,
|
||||||
Bool,
|
Bool,
|
||||||
INum,
|
INum,
|
||||||
|
|
@ -32,6 +26,7 @@ pub struct SchemaCommand {
|
||||||
pub kind: SchemaKind,
|
pub kind: SchemaKind,
|
||||||
pub long: &'static str,
|
pub long: &'static str,
|
||||||
pub short: Option<char>,
|
pub short: Option<char>,
|
||||||
|
pub required: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
|
|
@ -107,14 +102,17 @@ where
|
||||||
T: CliArg,
|
T: CliArg,
|
||||||
{
|
{
|
||||||
fn add_schema(schema: &mut Schema) -> Result<()> {
|
fn add_schema(schema: &mut Schema) -> Result<()> {
|
||||||
let kind = T::Content::kind();
|
|
||||||
let long = T::long();
|
|
||||||
let short = T::short();
|
let short = T::short();
|
||||||
let command = SchemaCommand { kind, long, short };
|
let command = SchemaCommand {
|
||||||
if let Some(short_name) = short {
|
kind: T::Content::kind(),
|
||||||
schema.add_short_command(short_name, command)?;
|
long: T::long(),
|
||||||
|
short,
|
||||||
|
required: T::required(),
|
||||||
|
};
|
||||||
|
if let Some(short) = short {
|
||||||
|
schema.add_short_command(short, command)?;
|
||||||
}
|
}
|
||||||
schema.add_command(long, command)
|
schema.add_command(T::long(), command)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -132,9 +130,10 @@ mod test {
|
||||||
fn one_command_schema() {
|
fn one_command_schema() {
|
||||||
let schema = Schema::create::<OutFile>().unwrap();
|
let schema = Schema::create::<OutFile>().unwrap();
|
||||||
let out_file = SchemaCommand {
|
let out_file = SchemaCommand {
|
||||||
kind: SchemaKind::Optional(SchemaKindType::String),
|
kind: SchemaKind::String,
|
||||||
long: "output",
|
long: "output",
|
||||||
short: Some('o'),
|
short: Some('o'),
|
||||||
|
required: false,
|
||||||
};
|
};
|
||||||
assert_eq!(schema.longs.get("output"), Some(&out_file));
|
assert_eq!(schema.longs.get("output"), Some(&out_file));
|
||||||
assert_eq!(schema.shorts.get(&'o'), Some(&out_file));
|
assert_eq!(schema.shorts.get(&'o'), Some(&out_file));
|
||||||
|
|
@ -145,14 +144,16 @@ mod test {
|
||||||
fn two_command_schema() {
|
fn two_command_schema() {
|
||||||
let schema = Schema::create::<(OutFile, Force)>().unwrap();
|
let schema = Schema::create::<(OutFile, Force)>().unwrap();
|
||||||
let out_file = SchemaCommand {
|
let out_file = SchemaCommand {
|
||||||
kind: SchemaKind::Optional(SchemaKindType::String),
|
kind: SchemaKind::String,
|
||||||
long: "output",
|
long: "output",
|
||||||
short: Some('o'),
|
short: Some('o'),
|
||||||
|
required: false,
|
||||||
};
|
};
|
||||||
let force = SchemaCommand {
|
let force = SchemaCommand {
|
||||||
kind: SchemaKind::Required(SchemaKindType::Bool),
|
kind: SchemaKind::Bool,
|
||||||
long: "force",
|
long: "force",
|
||||||
short: Some('f'),
|
short: Some('f'),
|
||||||
|
required: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(schema.longs.get("output"), Some(&out_file));
|
assert_eq!(schema.longs.get("output"), Some(&out_file));
|
||||||
|
|
@ -168,19 +169,22 @@ mod test {
|
||||||
fn three_command_schema() {
|
fn three_command_schema() {
|
||||||
let schema = Schema::create::<(OutFile, (Force, SetUpstream))>().unwrap();
|
let schema = Schema::create::<(OutFile, (Force, SetUpstream))>().unwrap();
|
||||||
let out_file = SchemaCommand {
|
let out_file = SchemaCommand {
|
||||||
kind: SchemaKind::Optional(SchemaKindType::String),
|
kind: SchemaKind::String,
|
||||||
long: "output",
|
long: "output",
|
||||||
short: Some('o'),
|
short: Some('o'),
|
||||||
|
required: false,
|
||||||
};
|
};
|
||||||
let force = SchemaCommand {
|
let force = SchemaCommand {
|
||||||
kind: SchemaKind::Required(SchemaKindType::Bool),
|
kind: SchemaKind::Bool,
|
||||||
long: "force",
|
long: "force",
|
||||||
short: Some('f'),
|
short: Some('f'),
|
||||||
|
required: true,
|
||||||
};
|
};
|
||||||
let set_upstream = SchemaCommand {
|
let set_upstream = SchemaCommand {
|
||||||
kind: SchemaKind::Required(SchemaKindType::String),
|
kind: SchemaKind::String,
|
||||||
long: "set-upstream",
|
long: "set-upstream",
|
||||||
short: None,
|
short: None,
|
||||||
|
required: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(schema.longs.get("output"), Some(&out_file));
|
assert_eq!(schema.longs.get("output"), Some(&out_file));
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue