Skip to content

Commit 9e5d13d

Browse files
RawkMattias Wallin
authored andcommitted
feat(cli) Add --stdout and --no-out
Add two new args: - `--stdout`: Output generated bindings to stdout. - `--no-out`: Do not output generated bindings. Useful when checking.
1 parent 6db2aed commit 9e5d13d

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

rasn-compiler/src/bin.rs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@ use std::process::ExitCode;
33

44
use clap::{arg, command, Parser};
55
use colored::Colorize;
6-
use rasn_compiler::{RasnCompiler, TsCompiler};
6+
use rasn_compiler::{OutputMode, RasnCompiler, TsCompiler};
77
use walkdir::WalkDir;
88

99
#[derive(clap::Parser, Debug)]
1010
#[command(author, version, about, long_about = None)]
1111
pub struct CompilerArgs {
12-
#[clap(flatten)]
12+
#[clap(flatten, next_help_heading = "Input")]
1313
source: SourceArgsGroup,
1414

15-
/// Set the output path for the generated rust module
16-
#[arg(short, long, default_value = ".")]
17-
output_path: PathBuf,
15+
#[clap(flatten, next_help_heading = "Output")]
16+
output: OutputArgGroup,
1817

1918
/// Specify which compiler backend to use
2019
#[arg(short, long, default_value = "rasn")]
@@ -26,14 +25,34 @@ pub struct CompilerArgs {
2625
pub struct SourceArgsGroup {
2726
/// Specify a directory for the compiler to search for ASN1 modules.
2827
/// The compiler will search recursively for `.asn` and `.asn1` files
29-
#[arg(short, long)]
28+
#[arg(short, long, value_name = "DIR")]
3029
directory: Option<PathBuf>,
3130

3231
/// Add an ASN1 module by path. Multiple modules can be added by appending "-m PATH_TO_MODULE"
33-
#[arg(short, long = "module", num_args(0..))]
32+
#[arg(short, long = "module", value_name="FILE", num_args(0..))]
3433
module_files: Vec<PathBuf>,
3534
}
3635

36+
#[derive(clap::Args, Debug)]
37+
#[group(required = false, multiple = false)]
38+
pub struct OutputArgGroup {
39+
/// Write all compiled modules to a single file at PATH.
40+
///
41+
/// If PATH is a directory, a default filename will be used in that directory.
42+
///
43+
/// If no output is specified, a default filename in current directory will be used.
44+
#[arg(short, long, value_name = "PATH")]
45+
output_path: Option<PathBuf>,
46+
47+
/// Write all compiled modules to stdout.
48+
#[arg(long)]
49+
stdout: bool,
50+
51+
/// Do not write anything, only check.
52+
#[arg(long)]
53+
no_output: bool,
54+
}
55+
3756
#[derive(clap::ValueEnum, Debug, Clone, Copy, PartialEq, Eq)]
3857
enum BackendArg {
3958
/// Generate rust-bindings for the rasn framework
@@ -82,14 +101,15 @@ fn main() -> ExitCode {
82101
return ExitCode::FAILURE;
83102
}
84103

104+
let output = make_output_mode(args.output);
85105
let results = match args.backend {
86106
BackendArg::Rasn => RasnCompiler::new()
87107
.add_asn_sources_by_path(modules.into_iter())
88-
.set_output_path(args.output_path)
108+
.set_output_mode(output)
89109
.compile(),
90110
BackendArg::Typescript => TsCompiler::new()
91111
.add_asn_sources_by_path(modules.into_iter())
92-
.set_output_path(args.output_path)
112+
.set_output_mode(output)
93113
.compile(),
94114
};
95115

@@ -106,3 +126,18 @@ fn main() -> ExitCode {
106126
}
107127
}
108128
}
129+
130+
/// Create an [OutputConf] from command arguments, that can be used with
131+
/// [RasnCompiler::set_output].
132+
fn make_output_mode(args: OutputArgGroup) -> OutputMode {
133+
// Only zero or one output argument is allowed, and enforced by Clap.
134+
if let Some(v) = args.output_path {
135+
OutputMode::SingleFile(v)
136+
} else if args.stdout {
137+
OutputMode::Stdout
138+
} else if args.no_output {
139+
OutputMode::NoOutput
140+
} else {
141+
OutputMode::SingleFile(".".into())
142+
}
143+
}

0 commit comments

Comments
 (0)