Skip to content

Commit

Permalink
positional_os, argument_os are now positional and argument + os
Browse files Browse the repository at this point in the history
  • Loading branch information
pacak committed Sep 15, 2022
1 parent c287bb5 commit c674081
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 198 deletions.
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Change Log
## bpaf [0.5.8] - unreleased
## bpaf [0.6.0] - unreleased
- internal refactors
- documentation refactor: examples with input/output
- any - to capture anything
Expand Down
5 changes: 3 additions & 2 deletions bpaf_cauwugo/src/opts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{ffi::OsString, sync::Mutex};

use bpaf::{positional, positional_os, Bpaf, CompleteDecor, Parser};
use bpaf::{positional, Bpaf, CompleteDecor, Parser};
use cargo_metadata::{CargoOpt, Metadata, MetadataCommand};
use once_cell::sync::Lazy;

Expand Down Expand Up @@ -195,7 +195,8 @@ fn remember_release(release: bool) -> bool {
}

fn child_process_args() -> impl Parser<Vec<OsString>> {
positional_os("CHILD_ARG")
positional("CHILD_ARG")
.os()
.strict()
.many()
.complete(complete_binary_args)
Expand Down
4 changes: 0 additions & 4 deletions bpaf_derive/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ pub enum Name {
#[derive(Debug, Clone)]
enum ConsumerAttr {
Arg(LitStr),
ArgOs(LitStr),
Pos(LitStr),
PosOs(LitStr),
Switch,
Any(LitStr),
Flag(Box<Expr>, Box<Expr>),
Expand Down Expand Up @@ -243,9 +241,7 @@ impl ToTokens for ConsumerAttr {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
ConsumerAttr::Arg(arg) => quote!(argument(#arg)),
ConsumerAttr::ArgOs(arg) => quote!(argument_os(#arg)),
ConsumerAttr::Pos(arg) => quote!(positional(#arg)),
ConsumerAttr::PosOs(arg) => quote!(positional_os(#arg)),
ConsumerAttr::Switch => quote!(switch()),
ConsumerAttr::Any(arg) => quote!(any(#arg)),
ConsumerAttr::Flag(a, b) => quote!(flag(#a, #b)),
Expand Down
56 changes: 18 additions & 38 deletions bpaf_derive/src/field/named_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,9 @@ impl Field {
} else if keyword == "argument" {
check_stage(&mut stage, 2, &keyword)?;
res.consumer = Some(ConsumerAttr::Arg(parse_optional_arg(input)?));
} else if keyword == "argument_os" {
check_stage(&mut stage, 2, &keyword)?;
res.consumer = Some(ConsumerAttr::ArgOs(parse_optional_arg(input)?));
} else if keyword == "positional" {
check_stage(&mut stage, 2, &keyword)?;
res.consumer = Some(ConsumerAttr::Pos(parse_optional_arg(input)?));
} else if keyword == "positional_os" {
check_stage(&mut stage, 2, &keyword)?;
res.consumer = Some(ConsumerAttr::PosOs(parse_optional_arg(input)?));
} else if keyword == "any" {
check_stage(&mut stage, 2, &keyword)?;
let arg = parse_optional_arg(input)?;
Expand Down Expand Up @@ -204,6 +198,8 @@ impl Field {

//
// postpr
} else if keyword == "os" {
res.os = true;
} else if keyword == "guard" {
check_stage(&mut stage, 4, &keyword)?;
let _ = parenthesized!(content in input);
Expand All @@ -224,8 +220,6 @@ impl Field {
check_stage(&mut stage, 4, &keyword)?;
res.postpr
.push(PostprAttr::Parse(span, parse_ident(input)?));
} else if keyword == "os" {
res.os = true;
} else if keyword == "map" {
check_stage(&mut stage, 4, &keyword)?;
res.postpr.push(PostprAttr::Map(span, parse_ident(input)?));
Expand Down Expand Up @@ -292,9 +286,8 @@ impl Field {
// Do we even need to derive the name here?
if let Some(cons) = &self.consumer {
match cons {
ConsumerAttr::Any(_) | ConsumerAttr::Pos(_) | ConsumerAttr::PosOs(_) => return,
ConsumerAttr::Any(_) | ConsumerAttr::Pos(_) => return,
ConsumerAttr::Arg(_)
| ConsumerAttr::ArgOs(_)
| ConsumerAttr::Switch
| ConsumerAttr::Flag(_, _)
| ConsumerAttr::ReqFlag(_) => {}
Expand Down Expand Up @@ -375,7 +368,6 @@ impl Field {
match &self.consumer {
Some(cons) => match cons {
ConsumerAttr::Arg(_)
| ConsumerAttr::ArgOs(_)
| ConsumerAttr::Switch
| ConsumerAttr::Flag(_, _)
| ConsumerAttr::ReqFlag(_) => {
Expand All @@ -386,42 +378,30 @@ impl Field {
));
}
}
ConsumerAttr::Pos(_) | ConsumerAttr::PosOs(_) | ConsumerAttr::Any(_) => {}
ConsumerAttr::Pos(_) | ConsumerAttr::Any(_) => {}
},
None => {
self.consumer = Some(match (is_os, self.naming.is_empty()) {
(true, true) => ConsumerAttr::PosOs(arg),
(true, false) => ConsumerAttr::ArgOs(arg),
(false, true) => ConsumerAttr::Pos(arg),
(false, false) => ConsumerAttr::Arg(arg),
});
if self.naming.is_empty() {
self.consumer = Some(ConsumerAttr::Pos(arg));
} else {
self.consumer = Some(ConsumerAttr::Arg(arg));
}
}
}

if derive_postpr {
let string = parse_quote!(String);
if let Some(ConsumerAttr::Any(_)) = &self.consumer {
if is_os || self.os {
self.postpr
.insert(0, PostprAttr::Tokens(ty.span(), quote!(os())));
}
if !is_os && ty != string {
if is_os || self.os {
self.postpr
.insert(0, PostprAttr::Tokens(ty.span(), quote!(os())));
if ty != parse_quote!(OsString) {
self.postpr
.insert(0, PostprAttr::FromStr(ty.span(), Box::new(ty)));
.insert(1, PostprAttr::Tokens(ty.span(), quote!(map(#ty::from))));
}
return Ok(());
}

self.postpr.insert(
0,
if is_os {
PostprAttr::Tokens(ty.span(), quote!(map(#ty::from)))
} else if ty != string {
PostprAttr::FromStr(ty.span(), Box::new(ty))
} else {
return Ok(());
},
);
if !is_os && ty != parse_quote!(String) {
self.postpr
.insert(0, PostprAttr::FromStr(ty.span(), Box::new(ty)));
}
}

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions bpaf_derive/src/field_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ fn check_many_files_implicit() {
files: Vec<std::path::PathBuf>
};
let output = quote! {
::bpaf::long("files").argument_os("ARG").map(std::path::PathBuf::from).many()
::bpaf::long("files").argument("ARG").os().map(std::path::PathBuf::from).many()
};
assert_eq!(input.to_token_stream().to_string(), output.to_string());
}
Expand All @@ -238,7 +238,7 @@ fn check_option_file_implicit() {
files: Option<PathBuf>
};
let output = quote! {
::bpaf::long("files").argument_os("ARG").map(PathBuf::from).optional()
::bpaf::long("files").argument("ARG").os().map(PathBuf::from).optional()
};
assert_eq!(input.to_token_stream().to_string(), output.to_string());
}
Expand Down
20 changes: 10 additions & 10 deletions bpaf_derive/src/top_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ fn unnamed_struct() {
#[allow (unused_imports)]
use ::bpaf::Parser;
{
let f0 = ::bpaf::positional_os("ARG").help("help").map(PathBuf::from);
let f0 = ::bpaf::positional("ARG").help("help").os().map(PathBuf::from);
::bpaf::construct!(Opt(f0))
}
.to_options()
Expand All @@ -222,7 +222,7 @@ fn unnamed_enum() {
#[allow (unused_imports)]
use ::bpaf::Parser;
{
let f0 = ::bpaf::positional_os("ARG").map(PathBuf::from);
let f0 = ::bpaf::positional("ARG").os().map(PathBuf::from);
let f1 = ::bpaf::positional("ARG").from_str::<usize>();
::bpaf::construct!(Opt1::Con1(f0, f1))
}
Expand Down Expand Up @@ -303,7 +303,7 @@ fn help_generation() {
#[allow (unused_imports)]
use ::bpaf::Parser;
{
let f0 = ::bpaf::positional_os("ARG").map(PathBuf::from);
let f0 = ::bpaf::positional("ARG").os().map(PathBuf::from);
::bpaf::construct!(Opt(f0))
}
.to_options()
Expand Down Expand Up @@ -407,7 +407,7 @@ fn version_with_commands_with_cargo_helper() {
fn named_to_positional_with_metavar() {
let top: Top = parse_quote! {
struct Options {
#[bpaf(positional_os("PATH"))]
#[bpaf(positional("PATH"), os)]
path: PathBuf,
}

Expand All @@ -418,7 +418,7 @@ fn named_to_positional_with_metavar() {
#[allow (unused_imports)]
use ::bpaf::Parser;
{
let path = ::bpaf::positional_os("PATH").map(PathBuf::from);
let path = ::bpaf::positional("PATH").os().map(PathBuf::from);
::bpaf::construct!(Options { path })
}
}
Expand All @@ -430,7 +430,7 @@ fn named_to_positional_with_metavar() {
fn named_to_positional_without_metavar() {
let top: Top = parse_quote! {
struct Options {
#[bpaf(positional_os)]
#[bpaf(positional, os)]
path: PathBuf,
}

Expand All @@ -441,7 +441,7 @@ fn named_to_positional_without_metavar() {
#[allow (unused_imports)]
use ::bpaf::Parser;
{
let path = ::bpaf::positional_os("ARG").map(PathBuf::from);
let path = ::bpaf::positional("ARG").os().map(PathBuf::from);
::bpaf::construct!(Options { path })
}
}
Expand All @@ -462,7 +462,7 @@ fn comp_visibility_struct() {
#[allow(unused_imports)]
use ::bpaf::Parser;
{
let path = ::bpaf::long("path").argument_os("ARG").map(PathBuf::from);
let path = ::bpaf::long("path").argument("ARG").os().map(PathBuf::from);
:: bpaf :: construct ! (Options { path })
}.complete_style(x)
}
Expand All @@ -485,7 +485,7 @@ fn comp_visibility_enum() {
#[allow(unused_imports)]
use ::bpaf::Parser;
{
let path = ::bpaf::long("path").argument_os("ARG").map(PathBuf::from);
let path = ::bpaf::long("path").argument("ARG").os().map(PathBuf::from);
:: bpaf :: construct ! (Foo::Bar { path })
}
.complete_style(x)
Expand All @@ -509,7 +509,7 @@ fn private_visibility() {
#[allow (unused_imports)]
use ::bpaf::Parser;
{
let path = ::bpaf::long("path").argument_os("ARG").map(PathBuf::from);
let path = ::bpaf::long("path").argument("ARG").os().map(PathBuf::from);
::bpaf::construct!(Options { path })
}
}
Expand Down
6 changes: 4 additions & 2 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ fn main() {
let output = short('o')
.long("output")
.help("output file")
.argument_os("OUTPUT")
.argument("OUTPUT")
.os()
.map(PathBuf::from);

// no magical name transmogrifications in combinatoric API
Expand All @@ -52,7 +53,8 @@ fn main() {
let file_to_proces = short('f')
.long("file")
.help("File to process")
.argument_os("FILE")
.argument("FILE")
.os()
.map(PathBuf::from);
let files_to_process = file_to_proces.many();

Expand Down
5 changes: 3 additions & 2 deletions examples/derive_show_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Options {
#[bpaf(external(parse_manifest_path))]
pub manifest_path: PathBuf,
/// Custom target directory for generated artifacts
#[bpaf(argument_os("DIR"))]
#[bpaf(argument("DIR"))]
pub target_dir: Option<PathBuf>,
/// Package to use if ambigous
#[bpaf(long, short, argument("SPEC"))]
Expand Down Expand Up @@ -51,7 +51,8 @@ fn verbosity() -> impl Parser<usize> {
fn parse_manifest_path() -> impl Parser<PathBuf> {
long("manifest-path")
.help("Path to Cargo.toml")
.argument_os("PATH")
.argument("PATH")
.os()
.map(PathBuf::from)
.parse(|p| {
if p.is_absolute() {
Expand Down
2 changes: 1 addition & 1 deletion examples/positional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
.argument("VAL")
.from_str::<u32>()
.fallback(42);
let files = positional_os("FILE").map(PathBuf::from).many();
let files = positional("FILE").os().map(PathBuf::from).many();
let opts = construct!(Options { value, files }).to_options().run();

println!("{:#?}", opts);
Expand Down
2 changes: 1 addition & 1 deletion examples/positional_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct Options {
/// Mysterious value
#[bpaf(argument("VAL"), fallback(42))]
value: u32,
#[bpaf(positional_os("FILE"))]
#[bpaf(positional("FILE"))]
files: Vec<PathBuf>,
}

Expand Down
6 changes: 4 additions & 2 deletions examples/top_to_bottom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ fn output() -> impl Parser<PathBuf> {
short('o')
.long("output")
.help("output file")
.argument_os("OUTPUT")
.argument("OUTPUT")
.os()
.map(PathBuf::from)
}

Expand All @@ -74,7 +75,8 @@ fn files_to_process() -> impl Parser<Vec<PathBuf>> {
short('f')
.long("file")
.help("File to process")
.argument_os("FILE")
.argument("FILE")
.os()
.map(PathBuf::from)
.many()
}
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@
//! `--test` that can be both [`switch`](Named::switch) and [`argument`](Named::argument) you
//! should put the argument one first.
//!
//! You must place [`positional`] and [`positional_os`] items at the end of a structure
//! in derive API or consume them as last arguments in derive API.
//! You must place [`positional`] items at the end of a structure in derive API or consume them
//! as last arguments in derive API.
//! # Dynamic shell completion
//!
Expand Down Expand Up @@ -350,7 +350,7 @@ pub mod parsers {
//!
//! In most cases you won't be using those names directly, they are only listed here to provide
//! access to documentation for member functions
pub use crate::params::{Command, Named, Positional};
pub use crate::params::{Command, Named, ParsePositional};
pub use crate::structs::{ParseMany, ParseOptional, ParseSome};
}

Expand All @@ -369,7 +369,7 @@ pub use crate::info::OptionParser;
pub use crate::meta::Meta;

#[doc(inline)]
pub use crate::params::{any, command, env, long, positional, positional_os, short};
pub use crate::params::{any, command, env, long, positional, short};

#[cfg(doc)]
pub(self) use crate::parsers::Named;
Expand Down
Loading

0 comments on commit c674081

Please sign in to comment.