1pub mod script;
2pub mod shell;
3pub mod value;
4
5pub(crate) use aopt_core as acore;
6
7pub(crate) const SHELL_BASH: &str = "bash";
8pub(crate) const SHELL_FISH: &str = "fish";
9pub(crate) const SHELL_ZSH: &str = "zsh";
10pub(crate) const SHELL_PSH: &str = "powershell";
11
12pub use acore::error;
13pub use acore::failure;
14
15use std::borrow::Cow;
16use std::ffi::OsStr;
17use std::ffi::OsString;
18
19use acore::opt::Opt;
20use acore::Error;
21use acore::HashMap;
22
23use crate::value::Values;
24
25pub struct Context<'a, O>
26where
27 O: Opt,
28{
29 pub args: &'a [OsString],
30
31 pub arg: Cow<'a, OsStr>,
33
34 pub val: Option<Cow<'a, OsStr>>,
36
37 pub prev: Cow<'a, OsStr>,
39
40 pub values: HashMap<String, Box<dyn Values<O, Err = Error>>>,
42}
43
44impl<'a, O> Context<'a, O>
45where
46 O: Opt,
47{
48 pub fn new(args: &'a [OsString], curr: &'a OsString, prev: &'a OsString) -> Self {
49 use std::borrow::Cow;
50
51 let mut incomplete_arg = Cow::Borrowed(curr.as_ref());
52 let mut incomplete_val = None;
53
54 if let Some((opt, val)) = aopt_core::str::split_once(curr, '=') {
55 incomplete_arg = opt;
56 incomplete_val = Some(val);
57 }
58
59 Self {
60 args,
61 arg: incomplete_arg,
62 val: incomplete_val,
63 prev: std::borrow::Cow::Borrowed(prev),
64 values: HashMap::default(),
65 }
66 }
67
68 pub fn with_values<V>(mut self, name: &str, v: V) -> Self
69 where
70 V: Values<O> + 'static,
71 {
72 self.set_values(name, v);
73 self
74 }
75
76 pub fn set_values<V>(&mut self, name: &str, v: V) -> &mut Self
77 where
78 V: Values<O> + 'static,
79 {
80 self.values
81 .insert(name.to_string(), Box::new(crate::value::wrap(v)));
82 self
83 }
84}