-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcli.rs
More file actions
142 lines (124 loc) · 4.94 KB
/
cli.rs
File metadata and controls
142 lines (124 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
use lemmy_help::{parser, vimdoc::VimDoc, FromEmmy, Layout, Settings};
use lexopt::{
Arg::{Long, Short, Value},
Parser, ValueExt,
};
use std::{fs::read_to_string, path::PathBuf, str::FromStr};
pub const NAME: &str = env!("CARGO_PKG_NAME");
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const DESC: &str = env!("CARGO_PKG_DESCRIPTION");
pub const AUTHOR: &str = env!("CARGO_PKG_AUTHORS");
pub struct Cli {
modeline: bool,
settings: Settings,
files: Vec<PathBuf>,
}
impl Default for Cli {
fn default() -> Self {
Self {
modeline: true,
settings: Settings::default(),
files: vec![],
}
}
}
impl Cli {
pub fn new() -> Result<Self, lexopt::Error> {
let mut c = Cli::default();
let mut parser = Parser::from_env();
while let Some(arg) = parser.next()? {
match arg {
Short('v') | Long("version") => {
println!("{NAME} {VERSION}");
std::process::exit(0);
}
Short('h') | Long("help") => {
Self::help();
std::process::exit(0);
}
Short('l') | Long("layout") => {
let layout = parser.value()?.string()?;
c.settings.layout =
Layout::from_str(&layout).map_err(|_| lexopt::Error::UnexpectedValue {
option: "layout".into(),
value: layout.into(),
})?;
}
Short('i') | Long("indent") => {
c.settings.indent_width = parser.value()?.parse()?;
}
Short('M') | Long("no-modeline") => c.modeline = false,
Short('f') | Long("prefix-func") => c.settings.prefix_func = true,
Short('a') | Long("prefix-alias") => c.settings.prefix_alias = true,
Short('c') | Long("prefix-class") => c.settings.prefix_class = true,
Short('t') | Long("prefix-type") => c.settings.prefix_type = true,
Long("expand-opt") => c.settings.expand_opt = true,
Value(val) => {
let file = PathBuf::from(&val);
if !file.is_file() {
return Err(lexopt::Error::UnexpectedArgument(
format!("{} is not a file!", file.display()).into(),
));
}
c.files.push(file)
}
_ => return Err(arg.unexpected()),
}
}
Ok(c)
}
pub fn run(self) {
let mut help_doc = String::new();
// FIXME: toc entries
for f in self.files {
let source = read_to_string(f).unwrap();
let ast = parser(&source);
let doc = VimDoc::from_emmy(&ast, &self.settings);
help_doc.push_str(&doc.to_string());
}
print!("{help_doc}");
if self.modeline {
println!("vim:tw=78:ts=8:noet:ft=help:norl:");
}
}
#[inline]
pub fn help() {
print!(
r#"{NAME} {VERSION}
{AUTHOR}
{DESC}
USAGE:
{NAME} [FLAGS] [OPTIONS] <FILES>...
ARGS:
<FILES>... Path to lua files
FLAGS:
-h, --help Print help information
-v, --version Print version information
-M, --no-modeline Don't print modeline at the end
-f, --prefix-func Prefix function name with ---@mod name
-a, --prefix-alias Prefix ---@alias tag with return/---@mod name
-c, --prefix-class Prefix ---@class tag with return/---@mod name
-t, --prefix-type Prefix ---@type tag with ---@mod name
--expand-opt Expand '?' (optional) to 'nil' type
OPTIONS:
-i, --indent <u8> Controls the indent width [default: 4]
-l, --layout <layout> Vimdoc text layout [default: 'default']
- "default" : Default layout
- "compact[:n=0]" : Aligns [desc] with <type>
and uses {{n}}, if provided, to indent the
following new lines. This option only
affects ---@field and ---@param tags
- "mini[:n=0]" : Aligns [desc] from the start
and uses {{n}}, if provided, to indent the
following new lines. This option affects
---@field, ---@param and ---@return tags
USAGE:
{NAME} /path/to/first.lua /path/to/second.lua > doc/PLUGIN_NAME.txt
{NAME} -c -a /path/to/{{first,second,third}}.lua > doc/PLUGIN_NAME.txt
{NAME} --layout compact:2 /path/to/plugin.lua > doc/PLUGIN_NAME.txt
NOTES:
- The order of parsing + rendering is relative to the given files
"#
);
}
}