Struct hyper_scripter::args::Root
source · Fields§
§root_args: RootArgs§subcmd: Option<Subs>Implementations§
source§impl Root
impl Root
sourcepub fn set_home_unless_from_alias(&self, create_on_missing: bool) -> Result
pub fn set_home_unless_from_alias(&self, create_on_missing: bool) -> Result
若帶了 –no-alias 選項,或是補全模式,我們可以把設定腳本之家(以及載入設定檔)的時間再推遲 在補全模式中意義重大,因為使用者可能會用 -H 指定別的腳本之家
Examples found in repository?
src/util/completion_util.rs (line 82)
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
pub async fn handle_completion(comp: Completion, repo: &mut Option<ScriptRepo>) -> Result {
match comp {
Completion::LS { name, args } => {
let mut new_root = match Root::try_parse_from(args) {
Ok(Root {
subcmd: Some(Subs::Tags(_) | Subs::Types(_)),
..
}) => {
// TODO: 在補全腳本中處理,而不要在這邊
return Err(Error::Completion);
}
Ok(t) => t,
Err(e) => {
log::warn!("補全時出錯 {}", e);
// NOTE: -V 或 --help 也會走到這裡
return Err(Error::Completion);
}
};
log::info!("補完模式,參數為 {:?}", new_root);
new_root.set_home_unless_from_alias(false)?;
new_root.sanitize_flags();
*repo = Some(init_repo(new_root.root_args, false).await?);
let iter = repo.as_mut().unwrap().iter_mut(Visibility::Normal);
let scripts = if let Some(name) = name {
fuzz_arr(&name, iter).await?
} else {
let mut t: Vec<_> = iter.collect();
sort(&mut t);
t
};
print_iter(scripts.iter().map(|s| s.name.key()), " ");
}
Completion::NoSubcommand { args } => {
if let Ok(root) = parse_alias_root(&args) {
if root.subcmd.is_some() {
log::debug!("子命令 = {:?}", root.subcmd);
return Err(Error::Completion);
}
} // else: 解析錯誤當然不可能有子命令啦
}
Completion::Alias { args } => {
let root = parse_alias_root(&args)?;
if root.root_args.no_alias {
log::info!("無別名模式");
return Err(Error::Completion);
}
let home = path::compute_home_path_optional(root.root_args.hs_home.as_ref(), false)?;
let conf = Config::load(&home)?;
if let Some(new_args) = root.expand_alias(&args, &conf) {
print_iter(new_args, " ");
} else {
log::info!("並非別名");
return Err(Error::Completion);
};
}
Completion::Home { args } => {
let root = parse_alias_root(&args)?;
let home = root.root_args.hs_home.ok_or_else(|| Error::Completion)?;
print!("{}", home);
}
Completion::ParseRun { args } => {
let mut root = Root::try_parse_from(args).map_err(|e| {
log::warn!("補全時出錯 {}", e);
Error::Completion
})?;
root.sanitize()?;
match root.subcmd {
Some(Subs::Run {
script_query, args, ..
}) => {
let iter = std::iter::once(script_query.to_string())
.chain(args.into_iter().map(|s| to_display_args(s)));
print_iter(iter, " ");
}
res @ _ => {
log::warn!("非執行指令 {:?}", res);
return Err(Error::Completion);
}
}
}
}
Ok(())
}sourcepub fn sanitize_flags(&mut self)
pub fn sanitize_flags(&mut self)
Examples found in repository?
src/args/mod.rs (line 459)
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
pub fn sanitize(&mut self) -> Result {
match &mut self.subcmd {
Some(Subs::Other(args)) => {
let args = [APP_NAME, "run"]
.into_iter()
.chain(args.iter().map(|s| s.as_str()));
self.subcmd = Some(Subs::parse_from(args));
log::info!("執行模式 {:?}", self.subcmd);
}
Some(Subs::Help { args }) => {
print_help(args.iter())?;
}
Some(Subs::Tags(tags)) => {
tags.sanitize();
}
Some(Subs::Types(types)) => {
types.sanitize();
}
None => {
log::info!("無參數模式");
self.subcmd = Some(Subs::Edit {
edit_query: EditQuery::Query(Default::default()),
ty: None,
content: vec![],
tags: None,
fast: false,
no_template: false,
});
}
_ => (),
}
self.sanitize_flags();
Ok(())
}More examples
src/util/completion_util.rs (line 83)
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
pub async fn handle_completion(comp: Completion, repo: &mut Option<ScriptRepo>) -> Result {
match comp {
Completion::LS { name, args } => {
let mut new_root = match Root::try_parse_from(args) {
Ok(Root {
subcmd: Some(Subs::Tags(_) | Subs::Types(_)),
..
}) => {
// TODO: 在補全腳本中處理,而不要在這邊
return Err(Error::Completion);
}
Ok(t) => t,
Err(e) => {
log::warn!("補全時出錯 {}", e);
// NOTE: -V 或 --help 也會走到這裡
return Err(Error::Completion);
}
};
log::info!("補完模式,參數為 {:?}", new_root);
new_root.set_home_unless_from_alias(false)?;
new_root.sanitize_flags();
*repo = Some(init_repo(new_root.root_args, false).await?);
let iter = repo.as_mut().unwrap().iter_mut(Visibility::Normal);
let scripts = if let Some(name) = name {
fuzz_arr(&name, iter).await?
} else {
let mut t: Vec<_> = iter.collect();
sort(&mut t);
t
};
print_iter(scripts.iter().map(|s| s.name.key()), " ");
}
Completion::NoSubcommand { args } => {
if let Ok(root) = parse_alias_root(&args) {
if root.subcmd.is_some() {
log::debug!("子命令 = {:?}", root.subcmd);
return Err(Error::Completion);
}
} // else: 解析錯誤當然不可能有子命令啦
}
Completion::Alias { args } => {
let root = parse_alias_root(&args)?;
if root.root_args.no_alias {
log::info!("無別名模式");
return Err(Error::Completion);
}
let home = path::compute_home_path_optional(root.root_args.hs_home.as_ref(), false)?;
let conf = Config::load(&home)?;
if let Some(new_args) = root.expand_alias(&args, &conf) {
print_iter(new_args, " ");
} else {
log::info!("並非別名");
return Err(Error::Completion);
};
}
Completion::Home { args } => {
let root = parse_alias_root(&args)?;
let home = root.root_args.hs_home.ok_or_else(|| Error::Completion)?;
print!("{}", home);
}
Completion::ParseRun { args } => {
let mut root = Root::try_parse_from(args).map_err(|e| {
log::warn!("補全時出錯 {}", e);
Error::Completion
})?;
root.sanitize()?;
match root.subcmd {
Some(Subs::Run {
script_query, args, ..
}) => {
let iter = std::iter::once(script_query.to_string())
.chain(args.into_iter().map(|s| to_display_args(s)));
print_iter(iter, " ");
}
res @ _ => {
log::warn!("非執行指令 {:?}", res);
return Err(Error::Completion);
}
}
}
}
Ok(())
}sourcepub fn sanitize(&mut self) -> Result
pub fn sanitize(&mut self) -> Result
Examples found in repository?
More examples
src/util/completion_util.rs (line 132)
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
pub async fn handle_completion(comp: Completion, repo: &mut Option<ScriptRepo>) -> Result {
match comp {
Completion::LS { name, args } => {
let mut new_root = match Root::try_parse_from(args) {
Ok(Root {
subcmd: Some(Subs::Tags(_) | Subs::Types(_)),
..
}) => {
// TODO: 在補全腳本中處理,而不要在這邊
return Err(Error::Completion);
}
Ok(t) => t,
Err(e) => {
log::warn!("補全時出錯 {}", e);
// NOTE: -V 或 --help 也會走到這裡
return Err(Error::Completion);
}
};
log::info!("補完模式,參數為 {:?}", new_root);
new_root.set_home_unless_from_alias(false)?;
new_root.sanitize_flags();
*repo = Some(init_repo(new_root.root_args, false).await?);
let iter = repo.as_mut().unwrap().iter_mut(Visibility::Normal);
let scripts = if let Some(name) = name {
fuzz_arr(&name, iter).await?
} else {
let mut t: Vec<_> = iter.collect();
sort(&mut t);
t
};
print_iter(scripts.iter().map(|s| s.name.key()), " ");
}
Completion::NoSubcommand { args } => {
if let Ok(root) = parse_alias_root(&args) {
if root.subcmd.is_some() {
log::debug!("子命令 = {:?}", root.subcmd);
return Err(Error::Completion);
}
} // else: 解析錯誤當然不可能有子命令啦
}
Completion::Alias { args } => {
let root = parse_alias_root(&args)?;
if root.root_args.no_alias {
log::info!("無別名模式");
return Err(Error::Completion);
}
let home = path::compute_home_path_optional(root.root_args.hs_home.as_ref(), false)?;
let conf = Config::load(&home)?;
if let Some(new_args) = root.expand_alias(&args, &conf) {
print_iter(new_args, " ");
} else {
log::info!("並非別名");
return Err(Error::Completion);
};
}
Completion::Home { args } => {
let root = parse_alias_root(&args)?;
let home = root.root_args.hs_home.ok_or_else(|| Error::Completion)?;
print!("{}", home);
}
Completion::ParseRun { args } => {
let mut root = Root::try_parse_from(args).map_err(|e| {
log::warn!("補全時出錯 {}", e);
Error::Completion
})?;
root.sanitize()?;
match root.subcmd {
Some(Subs::Run {
script_query, args, ..
}) => {
let iter = std::iter::once(script_query.to_string())
.chain(args.into_iter().map(|s| to_display_args(s)));
print_iter(iter, " ");
}
res @ _ => {
log::warn!("非執行指令 {:?}", res);
return Err(Error::Completion);
}
}
}
}
Ok(())
}Trait Implementations§
source§impl CommandFactory for Root
impl CommandFactory for Root
source§fn into_app_for_update<'b>() -> Command<'b>
fn into_app_for_update<'b>() -> Command<'b>
Deprecated, replaced with
CommandFactory::command_for_updatesource§impl FromArgMatches for Root
impl FromArgMatches for Root
source§fn from_arg_matches(__clap_arg_matches: &ArgMatches) -> Result<Self, Error>
fn from_arg_matches(__clap_arg_matches: &ArgMatches) -> Result<Self, Error>
source§fn from_arg_matches_mut(
__clap_arg_matches: &mut ArgMatches
) -> Result<Self, Error>
fn from_arg_matches_mut(
__clap_arg_matches: &mut ArgMatches
) -> Result<Self, Error>
source§fn update_from_arg_matches(
&mut self,
__clap_arg_matches: &ArgMatches
) -> Result<(), Error>
fn update_from_arg_matches(
&mut self,
__clap_arg_matches: &ArgMatches
) -> Result<(), Error>
Assign values from
ArgMatches to self.source§fn update_from_arg_matches_mut(
&mut self,
__clap_arg_matches: &mut ArgMatches
) -> Result<(), Error>
fn update_from_arg_matches_mut(
&mut self,
__clap_arg_matches: &mut ArgMatches
) -> Result<(), Error>
Assign values from
ArgMatches to self.source§impl Parser for Root
impl Parser for Root
source§fn parse_from<I, T>(itr: I) -> Selfwhere
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
fn parse_from<I, T>(itr: I) -> Selfwhere
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
Parse from iterator, exit on error
source§fn try_parse_from<I, T>(itr: I) -> Result<Self, Error>where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
fn try_parse_from<I, T>(itr: I) -> Result<Self, Error>where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
Parse from iterator, return Err on error.
source§fn update_from<I, T>(&mut self, itr: I)where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
fn update_from<I, T>(&mut self, itr: I)where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
Update from iterator, exit on error
source§fn try_update_from<I, T>(&mut self, itr: I) -> Result<(), Error>where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
fn try_update_from<I, T>(&mut self, itr: I) -> Result<(), Error>where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
Update from iterator, return Err on error.