cli.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. /**
  5. * Parse arguments from your Command Line Interface.
  6. *
  7. * This package is also accessible with `window.__TAURI__.cli` when `tauri.conf.json > build > withGlobalTauri` is set to true.
  8. * @module
  9. */
  10. import { invokeTauriCommand } from './helpers/tauri'
  11. interface ArgMatch {
  12. /**
  13. * string if takes value
  14. * boolean if flag
  15. * string[] or null if takes multiple values
  16. */
  17. value: string | boolean | string[] | null
  18. /**
  19. * Number of occurrences
  20. */
  21. occurrences: number
  22. }
  23. interface SubcommandMatch {
  24. name: string
  25. matches: CliMatches
  26. }
  27. interface CliMatches {
  28. args: { [name: string]: ArgMatch }
  29. subcommand: SubcommandMatch | null
  30. }
  31. /**
  32. * Parse the arguments provided to the current process and get the matches using the configuration defined `tauri.conf.json > tauri > cli`.
  33. *
  34. * @returns A promise resolving to the parsed arguments.
  35. */
  36. async function getMatches(): Promise<CliMatches> {
  37. return invokeTauriCommand<CliMatches>({
  38. __tauriModule: 'Cli',
  39. message: {
  40. cmd: 'cliMatches'
  41. }
  42. })
  43. }
  44. export type { ArgMatch, SubcommandMatch, CliMatches }
  45. export { getMatches }