This repository was archived by the owner on Sep 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathcli.rs
More file actions
228 lines (203 loc) · 6 KB
/
cli.rs
File metadata and controls
228 lines (203 loc) · 6 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// -*- coding: utf-8 -*-
// ------------------------------------------------------------------------------------------------
// Copyright © 2022, stack-graphs authors.
// Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
// ------------------------------------------------------------------------------------------------
//! This crate defines reusable subcommands for clap-based CLI implementations.
//!
//! ## Path loading CLIs
//!
//! Path loading CLIs load language configurations from the file system, based on
//! tree-sitter configuration files and provided arguments. Derive a path loading
//! CLI from as follows:
//!
//! ``` no_run
//! use clap::Parser;
//! use tree_sitter_stack_graphs::cli::path_loading::Subcommands;
//!
//! #[derive(Parser)]
//! #[clap(about, version)]
//! pub struct Cli {
//! #[clap(subcommand)]
//! subcommand: Subcommands,
//! }
//!
//! fn main() -> anyhow::Result<()> {
//! let cli = Cli::parse();
//! cli.subcommand.run()
//! }
//! ```
//!
//! ## Provided languages CLIs
//!
//! Provided languages CLIs use directly provided language configuration instances.
//! Derive a language configuration CLI as follows:
//!
//! ``` no_run
//! use clap::Parser;
//! use tree_sitter_stack_graphs::cli::provided_languages::Subcommands;
//!
//! #[derive(Parser)]
//! #[clap(about, version)]
//! pub struct Cli {
//! #[clap(subcommand)]
//! subcommand: Subcommands,
//! }
//!
//! fn main() -> anyhow::Result<()> {
//! let language_configurations = vec![/* add your language configurations here */];
//! let cli = Cli::parse();
//! cli.subcommand.run(language_configurations)
//! }
//! ```
pub(self) const MAX_PARSE_ERRORS: usize = 5;
pub mod analyze;
pub mod init;
pub mod load;
pub mod parse;
pub mod test;
mod util;
pub mod path_loading {
use clap::Subcommand;
use crate::cli::analyze::AnalyzeArgs;
use crate::cli::init::InitArgs;
use crate::cli::load::PathLoaderArgs;
use crate::cli::parse::ParseArgs;
use crate::cli::test::TestArgs;
#[derive(Subcommand)]
pub enum Subcommands {
Analyze(Analyze),
Init(Init),
Parse(Parse),
Test(Test),
}
impl Subcommands {
pub fn run(&self) -> anyhow::Result<()> {
match self {
Self::Analyze(cmd) => cmd.run(),
Self::Init(cmd) => cmd.run(),
Self::Parse(cmd) => cmd.run(),
Self::Test(cmd) => cmd.run(),
}
}
}
/// Analyze command
#[derive(clap::Parser)]
pub struct Analyze {
#[clap(flatten)]
load_args: PathLoaderArgs,
#[clap(flatten)]
analyze_args: AnalyzeArgs,
}
impl Analyze {
pub fn run(&self) -> anyhow::Result<()> {
let mut loader = self.load_args.get()?;
self.analyze_args.run(&mut loader)
}
}
/// Init command
#[derive(clap::Parser)]
pub struct Init {
#[clap(flatten)]
init_args: InitArgs,
}
impl Init {
pub fn run(&self) -> anyhow::Result<()> {
self.init_args.run()
}
}
/// Parse command
#[derive(clap::Parser)]
pub struct Parse {
#[clap(flatten)]
load_args: PathLoaderArgs,
#[clap(flatten)]
parse_args: ParseArgs,
}
impl Parse {
pub fn run(&self) -> anyhow::Result<()> {
let mut loader = self.load_args.get()?;
self.parse_args.run(&mut loader)
}
}
/// Test command
#[derive(clap::Parser)]
pub struct Test {
#[clap(flatten)]
load_args: PathLoaderArgs,
#[clap(flatten)]
test_args: TestArgs,
}
impl Test {
pub fn run(&self) -> anyhow::Result<()> {
let mut loader = self.load_args.get()?;
self.test_args.run(&mut loader)
}
}
}
pub mod provided_languages {
use clap::Subcommand;
use crate::cli::analyze::AnalyzeArgs;
use crate::cli::load::LanguageConfigurationsLoaderArgs;
use crate::cli::parse::ParseArgs;
use crate::cli::test::TestArgs;
use crate::loader::LanguageConfiguration;
#[derive(Subcommand)]
pub enum Subcommands {
Analyze(Analyze),
Parse(Parse),
Test(Test),
}
impl Subcommands {
pub fn run(&self, configurations: Vec<LanguageConfiguration>) -> anyhow::Result<()> {
match self {
Self::Analyze(cmd) => cmd.run(configurations),
Self::Parse(cmd) => cmd.run(configurations),
Self::Test(cmd) => cmd.run(configurations),
}
}
}
/// Analyze command
#[derive(clap::Parser)]
pub struct Analyze {
#[clap(flatten)]
load_args: LanguageConfigurationsLoaderArgs,
#[clap(flatten)]
analyze_args: AnalyzeArgs,
}
impl Analyze {
pub fn run(&self, configurations: Vec<LanguageConfiguration>) -> anyhow::Result<()> {
let mut loader = self.load_args.get(configurations)?;
self.analyze_args.run(&mut loader)
}
}
/// Parse command
#[derive(clap::Parser)]
pub struct Parse {
#[clap(flatten)]
load_args: LanguageConfigurationsLoaderArgs,
#[clap(flatten)]
parse_args: ParseArgs,
}
impl Parse {
pub fn run(&self, configurations: Vec<LanguageConfiguration>) -> anyhow::Result<()> {
let mut loader = self.load_args.get(configurations)?;
self.parse_args.run(&mut loader)
}
}
/// Test command
#[derive(clap::Parser)]
pub struct Test {
#[clap(flatten)]
load_args: LanguageConfigurationsLoaderArgs,
#[clap(flatten)]
test_args: TestArgs,
}
impl Test {
pub fn run(&self, configurations: Vec<LanguageConfiguration>) -> anyhow::Result<()> {
let mut loader = self.load_args.get(configurations)?;
self.test_args.run(&mut loader)
}
}
}