|
| 1 | +import { Colors } from '@qui-cli/colors'; |
| 2 | +import { Positionals } from '@qui-cli/core'; |
| 3 | +import { Log } from '@qui-cli/log'; |
1 | 4 | import * as Plugin from '@qui-cli/plugin'; |
2 | 5 |
|
3 | | -export type Configuration = Plugin.Configuration & {}; |
| 6 | +/** |
| 7 | + * The expected configuration parameters, which may the same as or different |
| 8 | + * from the command line options |
| 9 | + */ |
| 10 | +export type Configuration = Plugin.Configuration & { |
| 11 | + target?: string; |
| 12 | + text?: string; |
| 13 | + number?: number; |
| 14 | + flag?: boolean; |
| 15 | +}; |
4 | 16 |
|
| 17 | +/** |
| 18 | + * Configure positionals outside of the hooks, so that you don't run the risk of |
| 19 | + * having the configuration invoked multiple times, creating a series of |
| 20 | + * unanticipated positional arguments |
| 21 | + */ |
| 22 | +Positionals.require({ |
| 23 | + target: { |
| 24 | + description: 'A positional text argument' |
| 25 | + } |
| 26 | +}); |
| 27 | +Positionals.allowOnlyNamedArgs(); |
| 28 | +Positionals.requireAtLeast(0); |
| 29 | + |
| 30 | +/** A unique name for the command to identify it within the Plugin.Registrar */ |
5 | 31 | export const name = 'distributable-command'; |
6 | 32 |
|
7 | | -export async function run() {} |
| 33 | +const config: Configuration = { |
| 34 | + number: 2.171 |
| 35 | +}; |
| 36 | + |
| 37 | +/** |
| 38 | + * The command line optoons that the can be passed to the command |
| 39 | + * |
| 40 | + * @returns Plugin.Options …but don't declare that, because it is _way_ more |
| 41 | + * useful to have it typed as the literal object that it returns |
| 42 | + */ |
| 43 | +export function options() { |
| 44 | + return { |
| 45 | + man: [{ level: 1, text: 'Command Options' }], |
| 46 | + opt: { |
| 47 | + text: { |
| 48 | + description: 'A text parameter', |
| 49 | + short: 't', |
| 50 | + hint: Colors.quotedValue(`"argle bargle"`) |
| 51 | + } |
| 52 | + }, |
| 53 | + num: { |
| 54 | + number: { |
| 55 | + description: 'A numeric parameter', |
| 56 | + hint: Colors.value('3.14159'), |
| 57 | + /** |
| 58 | + * Setting defaults in the config variable above allows |
| 59 | + * pre-initialization updates to the defaults by other modules that |
| 60 | + * depend on this one |
| 61 | + */ |
| 62 | + default: config.number |
| 63 | + } |
| 64 | + }, |
| 65 | + flag: { |
| 66 | + flag: { |
| 67 | + description: 'A boolean parameter', |
| 68 | + short: 'f' |
| 69 | + } |
| 70 | + } |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Update the configuration (may be called by other modules and may be called |
| 76 | + * multiple times) |
| 77 | + */ |
| 78 | +export function configure(proposal: Configuration = {}) { |
| 79 | + for (const key in proposal) { |
| 80 | + if (proposal[key] !== undefined) { |
| 81 | + config[key] = proposal[key]; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** Initialize the command with the command line arguments */ |
| 87 | +export function init({ values }: Plugin.ExpectedArguments<typeof options>) { |
| 88 | + configure({ target: Positionals.get('target'), ...values }); |
| 89 | +} |
| 90 | + |
| 91 | +/** The actual "business logic" of the command */ |
| 92 | +export async function run() { |
| 93 | + Log.syntaxColor({ config }); |
| 94 | +} |
0 commit comments