Skip to content

Commit 0100df4

Browse files
add json argument deserialization
This allows structured arguments to be passed from external tooling (mod managers, etc.) in a way that closely mirrors normal positional arguments. JSON mode is enabled by passing the `--json` argument.
1 parent 2d50c2a commit 0100df4

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/cli.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
use std::path::PathBuf;
22

33
use clap::{Parser, Subcommand};
4+
use serde::{Deserialize, Serialize};
45

56
use crate::ts::package_reference::PackageReference;
67
use crate::ts::version::Version;
78
use crate::util::os::OS;
89

9-
#[derive(Parser, Debug)]
10+
#[derive(Parser, Serialize, Deserialize, Debug)]
1011
#[command(author, version, about, long_about = None)]
1112
pub struct Args {
1213
#[clap(subcommand)]
14+
#[serde(flatten)]
1315
pub commands: Commands,
1416
}
1517

1618
const DEFAULT_MANIFEST: &str = "./";
1719

18-
#[derive(Subcommand, Debug, Clone)]
20+
#[derive(Subcommand, Debug, Clone, Serialize, Deserialize)]
21+
#[serde(rename_all = "snake_case")]
1922
pub enum InitSubcommand {
2023
/// Creates a tcli project which can be used to build and publish a package.
2124
Project {
@@ -35,7 +38,8 @@ pub enum InitSubcommand {
3538
Profile,
3639
}
3740

38-
#[derive(Subcommand, Debug, Clone)]
41+
#[derive(Subcommand, Serialize, Deserialize, Debug, Clone)]
42+
#[serde(rename_all = "snake_case")]
3943
pub enum ListSubcommand {
4044
/// List the platforms tcli supports.
4145
Platforms {
@@ -67,7 +71,7 @@ pub enum ListSubcommand {
6771
},
6872
}
6973

70-
#[derive(Subcommand, Debug)]
74+
#[derive(Subcommand, Serialize, Deserialize, Debug)]
7175
pub enum ExternSubcommand {
7276
/// Get the path of a game with the specified label.
7377
GameData {
@@ -79,7 +83,8 @@ pub enum ExternSubcommand {
7983
},
8084
}
8185

82-
#[derive(Subcommand, Debug)]
86+
#[derive(Subcommand, Serialize, Deserialize, Debug)]
87+
#[serde(rename_all = "snake_case")]
8388
pub enum Commands {
8489
/// Initialize a new project configuration.
8590
Init {

src/main.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(dead_code)]
22

3-
use std::io;
3+
use std::{env, io};
44
use std::path::PathBuf;
55

66
use clap::Parser;
@@ -23,6 +23,7 @@ use crate::package::Package;
2323
use crate::project::lock::LockFile;
2424
use crate::project::overrides::ProjectOverrides;
2525
use crate::project::Project;
26+
use crate::ts::experimental;
2627
use crate::ui::reporter::IndicatifReporter;
2728

2829
mod cli;
@@ -50,7 +51,19 @@ async fn main() -> Result<(), Error> {
5051
std::fs::create_dir_all(TCLI_HOME.as_path())?;
5152
}
5253

53-
let test: Result<(), Error> = match Args::parse().commands {
54+
let args = env::args().collect::<Vec<_>>();
55+
let args = args
56+
.iter()
57+
.position(|x| x == "--json")
58+
.and_then(|x| {
59+
let json = &args.get(x + 1)
60+
.expect("Missing JSON argument.");
61+
serde_json::from_str(json)
62+
.expect("Recieved an invalid JSON object as an argument.")
63+
})
64+
.unwrap_or_else(Args::parse);
65+
66+
let test: Result<(), Error> = match args.commands {
5467
Commands::Init {
5568
command,
5669
overwrite,

0 commit comments

Comments
 (0)