|
| 1 | +import { strict as a } from 'assert' |
| 2 | +import CommandLineArgs from 'command-line-args' |
| 3 | +import fromTo from 'command-line-args/fromTo' |
| 4 | + |
| 5 | +const [test, only, skip] = [new Map(), new Map(), new Map()] |
| 6 | + |
| 7 | +/* TODO: Rewrite this set of CLA tests to use from-to */ |
| 8 | +/* Each element in optionDefinitions should be a from-to config set. The output of which can be cleaned and named separately, i.e. naming each result "file", "verbose" etc and fixing the type of each value. */ |
| 9 | + |
| 10 | +test.set('Input argv is not mutated', async function () { |
| 11 | + const argv = ['one', 'two', '--one', 'something', '--two'] |
| 12 | + // const optionDefinitions = [ |
| 13 | + // { |
| 14 | + // name: 'one', |
| 15 | + // from: arg => arg === '--one', |
| 16 | + // to: (valueIndex, arg) => valueIndex === 1 && !arg.startsWith('--'), |
| 17 | + // type: values => values[0] |
| 18 | + // } |
| 19 | + // ] |
| 20 | + // const cla = new CommandLineArgs(argv, optionDefinitions) |
| 21 | + // const result = cla.parse() |
| 22 | + // a.notEqual(cla.args, argv) |
| 23 | + // a.notEqual(cla.args.length, argv.length) |
| 24 | + |
| 25 | + const result = fromTo(argv, { |
| 26 | + from: '--one', |
| 27 | + to: (arg, index, argv, valueIndex) => valueIndex > 1 || arg.startsWith('--'), |
| 28 | + remove: true |
| 29 | + }) |
| 30 | + this.data = {argv, result} |
| 31 | +}) |
| 32 | + |
| 33 | +test.set('--option <no value>', async function () { |
| 34 | + const argv = ['one', 'two', '--one', '--two', 'three'] |
| 35 | + // const optionDefinitions = [ |
| 36 | + // { |
| 37 | + // name: 'one', |
| 38 | + // from: arg => arg === '--one', |
| 39 | + // to: (valueIndex, arg) => valueIndex === 1 && !arg.startsWith('--'), |
| 40 | + // type: values => values[0] |
| 41 | + // } |
| 42 | + // ] |
| 43 | + // const cla = new CommandLineArgs(argv, optionDefinitions) |
| 44 | + // const result = cla.parse() |
| 45 | + // // this.data = {result, cla} |
| 46 | + // a.deepEqual(result, { one: undefined }) |
| 47 | + // a.deepEqual(cla.args, ['one', 'two', '--two']) |
| 48 | + |
| 49 | + const result = fromTo(argv, { |
| 50 | + from: '--one', |
| 51 | + // to: console.log, |
| 52 | + to: (arg, index, argv, valueIndex) => { |
| 53 | + console.log(arg, index, valueIndex) |
| 54 | + return valueIndex === 1 && !arg.startsWith('--') |
| 55 | + }, |
| 56 | + remove: true |
| 57 | + }) |
| 58 | + this.data = { result, argv } |
| 59 | +}) |
| 60 | + |
| 61 | +skip.set('--option flag', async function () { |
| 62 | + const argv = ['one', 'two', '--one', '--two'] |
| 63 | + const optionDefinitions = [ |
| 64 | + { |
| 65 | + name: 'one', |
| 66 | + from: arg => arg === '--one', |
| 67 | + type: values => true |
| 68 | + } |
| 69 | + ] |
| 70 | + const cla = new CommandLineArgs(argv, optionDefinitions) |
| 71 | + const result = cla.parse() |
| 72 | + a.deepEqual(result, { one: true }) |
| 73 | + a.deepEqual(cla.args, ['one', 'two', '--two']) |
| 74 | +}) |
| 75 | + |
| 76 | +skip.set('--option flag not present', async function () { |
| 77 | + const argv = ['one', 'two', '--not-one', '--two'] |
| 78 | + const optionDefinitions = [ |
| 79 | + { |
| 80 | + name: 'one', |
| 81 | + from: arg => arg === '--one', |
| 82 | + type: values => true |
| 83 | + } |
| 84 | + ] |
| 85 | + const cla = new CommandLineArgs(argv, optionDefinitions) |
| 86 | + const result = cla.parse() |
| 87 | + a.deepEqual(result, {}) |
| 88 | + a.deepEqual(cla.args, ['one', 'two', '--not-one', '--two']) |
| 89 | +}) |
| 90 | + |
| 91 | +skip.set('--option <value>', async function () { |
| 92 | + const argv = ['one', 'two', '--one', 'something', '--two'] |
| 93 | + const optionDefinitions = [ |
| 94 | + { |
| 95 | + name: 'one', |
| 96 | + from: arg => arg === '--one', |
| 97 | + to: (valueIndex, arg) => valueIndex === 1 && !arg.startsWith('--'), |
| 98 | + type: values => values[0] |
| 99 | + } |
| 100 | + ] |
| 101 | + const cla = new CommandLineArgs(argv, optionDefinitions) |
| 102 | + const result = cla.parse() |
| 103 | + a.deepEqual(result, { one: 'something' }) |
| 104 | + a.deepEqual(cla.args, ['one', 'two', '--two']) |
| 105 | +}) |
| 106 | + |
| 107 | +skip.set('no name supplied: use the fromArg as the default', async function () { |
| 108 | + const argv = ['one', 'two', '--one', 'something', '--two'] |
| 109 | + const optionDefinitions = [ |
| 110 | + { |
| 111 | + from: arg => arg === '--one', |
| 112 | + to: (valueIndex, arg) => valueIndex === 1 && !arg.startsWith('--'), |
| 113 | + type: values => values[0] |
| 114 | + } |
| 115 | + ] |
| 116 | + const cla = new CommandLineArgs(argv, optionDefinitions) |
| 117 | + const result = cla.parse() |
| 118 | + a.deepEqual(result, { '--one': 'something' }) |
| 119 | + a.deepEqual(cla.args, ['one', 'two', '--two']) |
| 120 | +}) |
| 121 | + |
| 122 | +skip.set('Missing type: all args to the right of the fromArg returned', async function () { |
| 123 | + const argv = ['one', 'two', '--one', 'first', 'second', '--two'] |
| 124 | + const optionDefinitions = [ |
| 125 | + { |
| 126 | + from: arg => arg === '--one', |
| 127 | + to: (valueIndex, arg, index, argv) => argv[index + 1] && argv[index + 1].startsWith('--') |
| 128 | + } |
| 129 | + ] |
| 130 | + const cla = new CommandLineArgs(argv, optionDefinitions) |
| 131 | + const result = cla.parse() |
| 132 | + a.deepEqual(result, { '--one': ['first', 'second'] }) |
| 133 | + a.deepEqual(cla.args, ['one', 'two', '--two']) |
| 134 | +}) |
| 135 | + |
| 136 | +skip.set('name can be a function receiving the extraction matched by from and to', async function () { |
| 137 | + const argv = ['one', 'two', '--one', 'first', 'second', '--two'] |
| 138 | + const optionDefinitions = [ |
| 139 | + { |
| 140 | + from: arg => arg === '--one', |
| 141 | + to: (valueIndex, arg, index, argv) => argv[index + 1] && argv[index + 1].startsWith('--'), |
| 142 | + name: (extraction) => extraction.join('|') |
| 143 | + } |
| 144 | + ] |
| 145 | + const cla = new CommandLineArgs(argv, optionDefinitions) |
| 146 | + const result = cla.parse() |
| 147 | + a.deepEqual(result, { '--one|first|second': ['first', 'second'] }) |
| 148 | +}) |
| 149 | + |
| 150 | +skip.set('dynamic definition function receives fromArg', async function () { |
| 151 | + const argv = ['one', 'two', '--one', 'something', '--two'] |
| 152 | + const optionDefinitions = [ |
| 153 | + { |
| 154 | + from: arg => arg === '--one', |
| 155 | + def: fromArg => { |
| 156 | + a.equal(fromArg, '--one') |
| 157 | + return { |
| 158 | + to: (valueIndex, arg) => valueIndex === 1, |
| 159 | + name: 'one', |
| 160 | + type: values => values[0] |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + ] |
| 165 | + const cla = new CommandLineArgs(argv, optionDefinitions) |
| 166 | + const result = cla.parse() |
| 167 | + a.deepEqual(result, { one: 'something' }) |
| 168 | + a.deepEqual(cla.args, ['one', 'two', '--two']) |
| 169 | +}) |
| 170 | + |
| 171 | +skip.set('noFurtherThan', async function () { |
| 172 | + const argv = ['command1', 'arg', '--option', 'value', '--flag', 'command2', 'arg2'] |
| 173 | + const commands = ['command1', 'command2'] |
| 174 | + const optionDefinitions = [ |
| 175 | + { |
| 176 | + from: arg => commands.includes(arg), |
| 177 | + noFurtherThan: (valueIndex, arg, index, argv) => commands.includes(arg) |
| 178 | + } |
| 179 | + ] |
| 180 | + const cla = new CommandLineArgs(argv, optionDefinitions) |
| 181 | + const result = cla.parse() |
| 182 | + // this.data = result |
| 183 | + a.deepEqual(result, { |
| 184 | + command1: ['arg', '--option', 'value', '--flag'], |
| 185 | + command2: ['arg2'] |
| 186 | + }) |
| 187 | +}) |
| 188 | + |
| 189 | +skip.set('to not found: no args matched', async function () { |
| 190 | + const argv = ['command1', 'arg', '--option', 'value', '--flag'] |
| 191 | + const optionDefinitions = [ |
| 192 | + { |
| 193 | + from: arg => arg === 'command1', |
| 194 | + to: () => false |
| 195 | + } |
| 196 | + ] |
| 197 | + const cla = new CommandLineArgs(argv, optionDefinitions) |
| 198 | + const result = cla.parse() |
| 199 | + // this.data = result |
| 200 | + a.deepEqual(result, { |
| 201 | + command1: [] |
| 202 | + }) |
| 203 | +}) |
| 204 | + |
| 205 | +skip.set('noFurtherThan not found: all args matched until the end', async function () { |
| 206 | + const argv = ['command1', 'arg', '--option', 'value', '--flag'] |
| 207 | + const optionDefinitions = [ |
| 208 | + { |
| 209 | + from: arg => arg === 'command1', |
| 210 | + noFurtherThan: () => false |
| 211 | + } |
| 212 | + ] |
| 213 | + const cla = new CommandLineArgs(argv, optionDefinitions) |
| 214 | + const result = cla.parse() |
| 215 | + // this.data = result |
| 216 | + a.deepEqual(result, { |
| 217 | + command1: ['arg', '--option', 'value', '--flag'] |
| 218 | + }) |
| 219 | +}) |
| 220 | + |
| 221 | +export { test, only, skip } |
0 commit comments