forked from tauri-apps/plugins-workspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
72 lines (64 loc) · 1.54 KB
/
index.ts
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
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
/**
* Parse arguments from your Command Line Interface.
*
* @module
*/
import { invoke } from '@tauri-apps/api/core'
/**
* @since 2.0.0
*/
interface ArgMatch {
/**
* string if takes value
* boolean if flag
* string[] or null if takes multiple values
*/
value: string | boolean | string[] | null
/**
* Number of occurrences
*/
occurrences: number
}
/**
* @since 2.0.0
*/
interface SubcommandMatch {
name: string
matches: CliMatches
}
/**
* @since 2.0.0
*/
interface CliMatches {
args: Record<string, ArgMatch>
subcommand: SubcommandMatch | null
}
/**
* Parse the arguments provided to the current process and get the matches using the configuration defined [`tauri.cli`](https://tauri.app/v1/api/config/#tauriconfig.cli) in `tauri.conf.json`
*
* @example
* ```typescript
* import { getMatches } from '@tauri-apps/plugin-cli';
* const matches = await getMatches();
* if (matches.subcommand?.name === 'run') {
* // `./your-app run $ARGS` was executed
* const args = matches.subcommand?.matches.args
* if ('debug' in args) {
* // `./your-app run --debug` was executed
* }
* } else {
* const args = matches.args
* // `./your-app $ARGS` was executed
* }
* ```
*
* @since 2.0.0
*/
async function getMatches(): Promise<CliMatches> {
return await invoke('plugin:cli|cli_matches')
}
export type { ArgMatch, SubcommandMatch, CliMatches }
export { getMatches }