|
| 1 | +import {Interfaces} from '..' |
| 2 | + |
| 3 | +type Flag = Interfaces.Command.Flag |
| 4 | +type Flags = Flag[] |
| 5 | + |
| 6 | +/** |
| 7 | + * DocOpts - See http://docopt.org/. |
| 8 | + * |
| 9 | + * flag.exclusive: groups elements when one of the mutually exclusive cases is a required flag: (--apple | --orange) |
| 10 | + * flag.exclusive: groups elements when none of the mutually exclusive cases is required (optional flags): [--apple | --orange] |
| 11 | + * flag.dependsOn: specifies that if one element is present, then another one is required: (--apple --orange) |
| 12 | + * |
| 13 | + * @example |
| 14 | + * { |
| 15 | + * name: 'classnames', |
| 16 | + * required: true, |
| 17 | + * exclusive: ['suitenames'] |
| 18 | + * ... |
| 19 | + * },{ |
| 20 | + * name: 'suitenames', |
| 21 | + * type: 'array' |
| 22 | + * required: true |
| 23 | + * ... |
| 24 | + * } |
| 25 | + * |
| 26 | + * Results in: |
| 27 | + * Usage: <%= command.id %> (-n <string> | -s <array>) |
| 28 | + * |
| 29 | + * @example |
| 30 | + * { |
| 31 | + * name: 'classnames', |
| 32 | + * ... |
| 33 | + * excludes: ['suitenames'] |
| 34 | + * },{ |
| 35 | + * name: 'suitenames', |
| 36 | + * ... |
| 37 | + * } |
| 38 | + * |
| 39 | + * Results in: |
| 40 | + * Usage: <%= command.id %> [-n <string> | -s <string>] |
| 41 | + * |
| 42 | + * @example |
| 43 | + * { |
| 44 | + * name: 'classnames', |
| 45 | + * ... |
| 46 | + * dependsOn: ['suitenames'] |
| 47 | + * },{ |
| 48 | + * name: 'suitenames', |
| 49 | + * type: 'flag' |
| 50 | + * ... |
| 51 | + * } |
| 52 | + * |
| 53 | + * Results in: |
| 54 | + * Usage: <%= command.id %> (-n <string> -s) |
| 55 | + * |
| 56 | + * TODO: |
| 57 | + * - Support nesting, eg: |
| 58 | + * Usage: my_program (--either-this <and-that> | <or-this>) |
| 59 | + * Usage: my_program [(<one-argument> <another-argument>)] |
| 60 | + * |
| 61 | + */ |
| 62 | +export class DocOpts { |
| 63 | + private flagMap: {[index: string]: Flag} |
| 64 | + |
| 65 | + private flagList: Flags |
| 66 | + |
| 67 | + public constructor(private cmd: Interfaces.Command) { |
| 68 | + // Create a new map with references to the flags that we can manipulate. |
| 69 | + this.flagMap = {} |
| 70 | + this.flagList = Object.entries(cmd.flags || {}) |
| 71 | + .filter(([_, flag]) => !flag.hidden) |
| 72 | + .map(([name, flag]) => { |
| 73 | + this.flagMap[name] = flag |
| 74 | + return flag |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + public static generate(cmd: Interfaces.Command): string { |
| 79 | + return new DocOpts(cmd).toString() |
| 80 | + } |
| 81 | + |
| 82 | + public toString(): string { |
| 83 | + const opts = ['<%= command.id %>'] |
| 84 | + if (this.cmd.args) { |
| 85 | + opts.push(...this.cmd.args?.map(arg => `[${arg.name.toUpperCase()}]`)) |
| 86 | + } |
| 87 | + try { |
| 88 | + opts.push(...Object.values(this.groupFlagElements())) |
| 89 | + } catch (error) { |
| 90 | + // If there is an error, just return no usage so we don't fail command help. |
| 91 | + opts.push(...this.flagList.map(flag => { |
| 92 | + const name = flag.char ? `-${flag.char}` : `--${flag.name}` |
| 93 | + if (flag.type === 'boolean') return name |
| 94 | + return `${name}=<value>` |
| 95 | + })) |
| 96 | + } |
| 97 | + return opts.join(' ') |
| 98 | + } |
| 99 | + |
| 100 | + private groupFlagElements(): {[index: string]: string} { |
| 101 | + const elementMap: {[index: string]: string} = {} |
| 102 | + |
| 103 | + // Generate all doc opt elements for combining |
| 104 | + // Show required flags first |
| 105 | + this.generateElements(elementMap, this.flagList.filter(flag => flag.required)) |
| 106 | + // Then show optional flags |
| 107 | + this.generateElements(elementMap, this.flagList.filter(flag => !flag.required)) |
| 108 | + |
| 109 | + for (const flag of this.flagList) { |
| 110 | + if (Array.isArray(flag.dependsOn)) { |
| 111 | + this.combineElementsToFlag(elementMap, flag.name, flag.dependsOn, ' ') |
| 112 | + } |
| 113 | + |
| 114 | + if (Array.isArray(flag.exclusive)) { |
| 115 | + this.combineElementsToFlag(elementMap, flag.name, flag.exclusive, ' | ') |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Since combineElementsToFlag deletes the references in this.flags when it combines |
| 120 | + // them, this will go through the remaining list of uncombined elements. |
| 121 | + for (const remainingFlagName of Object.keys(this.flagMap)) { |
| 122 | + const remainingFlag = this.flagMap[remainingFlagName] || {} |
| 123 | + |
| 124 | + if (!remainingFlag.required) { |
| 125 | + elementMap[remainingFlag.name] = `[${elementMap[remainingFlag.name] || ''}]` |
| 126 | + } |
| 127 | + } |
| 128 | + return elementMap |
| 129 | + } |
| 130 | + |
| 131 | + private combineElementsToFlag( |
| 132 | + elementMap: {[index: string]: string}, |
| 133 | + flagName: string, |
| 134 | + flagNames: string[], |
| 135 | + unionString: string, |
| 136 | + ): void { |
| 137 | + if (!this.flagMap[flagName]) { |
| 138 | + return |
| 139 | + } |
| 140 | + let isRequired = this.flagMap[flagName]?.required |
| 141 | + if (typeof isRequired !== 'boolean' || !isRequired) { |
| 142 | + isRequired = flagNames.reduce( |
| 143 | + (required: boolean, toCombine) => required || this.flagMap[toCombine]?.required || false, |
| 144 | + false, |
| 145 | + ) |
| 146 | + } |
| 147 | + |
| 148 | + for (const toCombine of flagNames) { |
| 149 | + elementMap[flagName] = `${elementMap[flagName] || ''}${unionString}${elementMap[toCombine] || ''}` |
| 150 | + // We handled this flag, don't handle it again |
| 151 | + delete elementMap[toCombine] |
| 152 | + delete this.flagMap[toCombine] |
| 153 | + } |
| 154 | + if (isRequired) { |
| 155 | + elementMap[flagName] = `(${elementMap[flagName] || ''})` |
| 156 | + } else { |
| 157 | + elementMap[flagName] = `[${elementMap[flagName] || ''}]` |
| 158 | + } |
| 159 | + // We handled this flag, don't handle it again |
| 160 | + delete this.flagMap[flagName] |
| 161 | + } |
| 162 | + |
| 163 | + private generateElements(elementMap: {[index: string]: string} = {}, flagGroups: Flags): string[] { |
| 164 | + const elementStrs = [] |
| 165 | + for (const flag of flagGroups) { |
| 166 | + let type = '' |
| 167 | + // not all flags have short names |
| 168 | + const flagName = flag.char ? `-${flag.char}` : `--${flag.name}` |
| 169 | + if (flag.type === 'option') { |
| 170 | + type = flag.options ? ` ${flag.options.join('|')}` : ' <value>' |
| 171 | + } |
| 172 | + const element = `${flagName}${type}` |
| 173 | + elementMap[flag.name] = element |
| 174 | + elementStrs.push(element) |
| 175 | + } |
| 176 | + return elementStrs |
| 177 | + } |
| 178 | +} |
0 commit comments