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
use crate::prelude::*;

use crate::{ios_deploy::detect::DetectDevicesConfig, open::well_known::WellKnown, xcrun::simctl};

pub use app_path::AppPathArgs;
mod app_path;
pub use device_name::{DeviceSimulatorBootedArgs, DeviceSimulatorUnBootedArgs};
mod device_name;
pub use bundle_identifier::BundleIdentifierArgs;
mod bundle_identifier;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
#[command(propagate_version = true)]
pub struct CliArgs {
	#[command(flatten)]
	pub args: TopLevelCliArgs,

	#[command(subcommand)]
	pub command: Commands,
}

#[derive(Args, Debug)]
#[group(required = false, multiple = false)]
pub struct TopLevelCliArgs {
	/// Outputs data as JSON. Aliased to --json.
	/// Is global, hence can be used with any command/subcommand except help messages
	#[arg(long, global = true, group = "top_level_args", alias = "json")]
	machine: bool,

	/// Overrides the RUST_LOG env variable to use a very verbose log level
	#[arg(long, global = true, group = "top_level_args")]
	verbose: bool,

	/// Only displays warnings and errors
	#[arg(long, short, global = true, group = "top_level_args")]
	quiet: bool,
}

impl TopLevelCliArgs {
	pub fn machine(&self) -> bool {
		self.machine
	}

	pub fn human(&self) -> bool {
		!self.machine()
	}

	pub fn verbose(&self) -> bool {
		self.verbose
	}

	pub fn quiet(&self) -> bool {
		self.quiet
	}
}

#[derive(Subcommand, Debug)]
pub enum Commands {
	/// Used for setting up completions
	#[clap(subcommand)]
	Init(Init),

	#[clap(subcommand)]
	IosDeploy(IosDeploy),

	// #[clap(subcommand)]
	// CargoBundle(CargoBundle),
	#[clap(subcommand)]
	Security(Security),

	#[clap(subcommand)]
	Spctl(Spctl),

	#[clap(subcommand, name = "codesign")]
	CodeSign(CodeSign),

	#[clap(subcommand, name = "xcrun")]
	XcRun(XcRun),

	Open(#[clap(flatten)] Open),
}

#[derive(Subcommand, Debug)]
pub enum Init {
	#[clap(name = "nushell")]
	#[group(required = true)]
	NuShell {
		/// Automatically adds shell completions to `~/.apple-clis.nu`
		/// and configures your config.nu file to source it.
		/// This is the recommended approach.
		#[arg(long, group = "init")]
		auto: bool,

		/// Run `apple-clis init nushell --raw-script | save -f ~/.apple-clis.nu`
		/// Then add `source ~/.apple-clis.nu` to your config.nu file,
		/// E.g. `"source ~/.apple-clis.nu" | save --append $nu.config-path`
		#[arg(long, group = "init")]
		raw_script: bool,
	},
}

#[derive(Subcommand, Debug)]
pub enum IosDeploy {
	/// Spends a second to detect any already connected devices
	Detect {
		#[clap(flatten)]
		config: DetectDevicesConfig,
	},
	/// Uploads an app to a real device
	Upload {
		#[clap(flatten)]
		app_path: app_path::AppPathArgs,

		#[clap(flatten)]
		auto_detect_config: DetectDevicesConfig,
	},
}

#[derive(Subcommand, Debug)]
pub enum CargoBundle {
	/// Bundles the iOS app
	Ios,
}

#[derive(Subcommand, Debug)]
pub enum Security {
	Certs,
	Pems,
}

#[derive(Subcommand, Debug)]
pub enum CodeSign {
	/// Displays the code signature of the given file
	Display {
		#[clap(flatten)]
		app_path: AppPathArgs,
	},
	Sign {
		#[clap(flatten)]
		app_path: AppPathArgs,
	},
}

#[derive(Subcommand, Debug)]
pub enum Spctl {
	AssessApp {
		#[clap(flatten)]
		app_path: AppPathArgs,
	},
}

#[derive(Subcommand, Debug)]
pub enum XcRun {
	#[clap(subcommand)]
	Simctl(Simctl),
}

#[derive(Debug, ValueEnum, Clone)]
pub enum Booted {
	Booted,
}

#[derive(Subcommand, Debug)]
pub enum Simctl {
	List,
	Boot {
		#[clap(flatten)]
		simulator_id: DeviceSimulatorUnBootedArgs,
	},
	Install {
		#[clap(flatten)]
		booted_simulator: DeviceSimulatorBootedArgs,

		#[clap(flatten)]
		app_path: app_path::AppPathArgs,
	},
	Launch {
		#[clap(flatten)]
		booted_simulator: DeviceSimulatorBootedArgs,

		#[clap(flatten)]
		args: simctl::launch::CliLaunchArgs,
	},
}

#[derive(Args, Debug)]
pub struct Open {
	#[arg(long, value_enum)]
	pub well_known: WellKnown,
}