-
Notifications
You must be signed in to change notification settings - Fork 5
fix(lexarg): Iterate on the design #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
cd0aa89
docs(lexarg): Clean up the docs
epage dcf8339
fix(lexarg)!: Clarify Parser::next works with `Arg`
epage 17a3c2c
refactor(lexarg): Clarify next_value isn't general
epage b9222d5
refactor(lexarg): Clarify how flag_value works
epage 6767504
fix(lexarg)!: Clarify Parser::flag_value advances
epage 5838027
refactor(lexarg): Clarify behavior with asserts
epage 92dd93b
feat(lexarg): Add Parser::next_raw
epage 985214a
fix(lexarg)!: Remove Parser::bin in favor of Parser::next_raw
epage 9dca1eb
fix(lexarg): Allow '-' as a short
epage 46af6ea
fix(lexarg)!: Always allow = as a short flag
epage e25397d
fix(lexarg): Ensure Escape is reported after an option
epage 2fe9162
docs(design): Clarify another lexarg design decision
epage 57d10d1
refactor(lexarg): Remove extraneous state change
epage 43731fb
test(lexarg): Show bug with attached values
epage 2f08cf5
fix(lexarg): Don't skip ahead to = sign
epage b3866e4
refactor(lexarg); Ensure next_attached_value is standalone
epage e1fa243
feat(lexarg): Expose Parser::next_attached_value
epage a0c0427
feat(lexarg): Expose Parser::remaining_raw
epage 6ab1334
refactor(lexarg): Clarify the error being specialized on index
epage 30d5757
fix(lexarg)!: Switch to `Short(str)`
epage a2740be
fix(lexarg)!: Switch to `Escape(str)`
epage aba8d47
feat(lexarg): Add Parser::peek_raw
epage 3646515
feat(lexarg): Make Arg copyable
epage 4dd2845
docs(lexarg): Remove bad example
epage a59ff1d
docs(lexarg): Clarify docs intro
epage 55096dc
refactor(lexarg): Prefer Self over naming the type
epage 8fde3b2
fix(lexarg)!: Pull out an ErrorContext builder
epage 0026ba8
feat(lexarg): Allow reporting the argument the operation failed within
epage 7442bd5
docs(lexarg): Split up operation in example
epage 19a1bdb
feat(lexarg): Allow reporting the argument that failed
epage 37dfada
docs(lexarg): Fix a bad value in an example
epage 313d30a
docs(lexarg): Pull out doc examples into targets
epage e40c077
feat(lexarg): Allow using Result in main
epage b6b8b49
docs(lexarg): Include short help
epage 6eb05e8
docs(lexarg): Add more design justification
epage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use lexarg_error::ErrorContext; | ||
use lexarg_error::Result; | ||
|
||
struct Args { | ||
thing: String, | ||
number: u32, | ||
shout: bool, | ||
} | ||
|
||
fn parse_args() -> Result<Args> { | ||
#![allow(clippy::enum_glob_use)] | ||
use lexarg::Arg::*; | ||
|
||
let mut thing = None; | ||
let mut number = 1; | ||
let mut shout = false; | ||
let raw = std::env::args_os().collect::<Vec<_>>(); | ||
let mut parser = lexarg::Parser::new(&raw); | ||
let bin_name = parser | ||
.next_raw() | ||
.expect("nothing parsed yet so no attached lingering") | ||
.expect("always at least one"); | ||
while let Some(arg) = parser.next_arg() { | ||
match arg { | ||
Short("n") | Long("number") => { | ||
let value = parser | ||
.next_flag_value() | ||
.ok_or_else(|| ErrorContext::msg("missing required value").within(arg))?; | ||
number = value | ||
.to_str() | ||
.ok_or_else(|| { | ||
ErrorContext::msg("invalid number") | ||
.unexpected(Value(value)) | ||
.within(arg) | ||
})? | ||
.parse() | ||
.map_err(|e| ErrorContext::msg(e).unexpected(Value(value)).within(arg))?; | ||
} | ||
Long("shout") => { | ||
shout = true; | ||
} | ||
Value(val) if thing.is_none() => { | ||
thing = Some( | ||
val.to_str() | ||
.ok_or_else(|| ErrorContext::msg("invalid string").unexpected(arg))?, | ||
); | ||
} | ||
Short("h") | Long("help") => { | ||
println!("Usage: hello [-n|--number=NUM] [--shout] THING"); | ||
std::process::exit(0); | ||
} | ||
_ => { | ||
return Err(ErrorContext::msg("unexpected argument") | ||
.unexpected(arg) | ||
.within(Value(bin_name)) | ||
.into()); | ||
} | ||
} | ||
} | ||
|
||
Ok(Args { | ||
thing: thing | ||
.ok_or_else(|| ErrorContext::msg("missing argument THING").within(Value(bin_name)))? | ||
.to_owned(), | ||
number, | ||
shout, | ||
}) | ||
} | ||
|
||
fn main() -> Result<()> { | ||
let args = parse_args()?; | ||
let mut message = format!("Hello {}", args.thing); | ||
if args.shout { | ||
message = message.to_uppercase(); | ||
} | ||
for _ in 0..args.number { | ||
println!("{message}"); | ||
} | ||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.