@trenskow/arguments-parser 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/index.js +29 -22
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -9,10 +9,21 @@
9
9
  import { basename } from 'path';
10
10
 
11
11
  import caseit from '@trenskow/caseit';
12
- import { default as isvalid, keyPaths, formalize } from 'isvalid';
12
+ import { default as isvalid, keyPaths, formalize, plugins } from 'isvalid';
13
13
  import print from '@trenskow/print';
14
14
 
15
- const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placeholder = '<>' } = {}) => {
15
+ plugins.use(() => ({
16
+ phase: 'pre',
17
+ supportsType: () => true,
18
+ validatorsForType: () => ({
19
+ description: ['string'],
20
+ defaultDescription: ['string']
21
+ }),
22
+ validate: (data) => data,
23
+ formalize: (schema) => schema
24
+ }));
25
+
26
+ const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placeholder = '<>', command } = {}) => {
16
27
 
17
28
  const [ opening, closing ] = placeholder.split('');
18
29
 
@@ -75,10 +86,15 @@ const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placehol
75
86
 
76
87
  try {
77
88
 
78
- args = args.slice(1);
79
- argvLevel++;
80
-
81
- return await commands[tool]({ args, argumentsParser: argumentsParser({ args, argvLevel, placeholder }) });
89
+ return await commands[tool]({
90
+ args: args.slice(1),
91
+ argumentsParser: argumentsParser({
92
+ args: args.slice(1),
93
+ argvLevel: argvLevel + 1,
94
+ placeholder,
95
+ command: commands[tool]
96
+ })
97
+ });
82
98
 
83
99
  } catch (error) {
84
100
  print.err(`${error.stack}`);
@@ -86,20 +102,11 @@ const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placehol
86
102
  }
87
103
 
88
104
  },
89
- options: async (schema, options = {}) => {
90
-
91
- schema = formalize(schema, {
92
- plugins: [() => ({
93
- phase: 'pre',
94
- supportsType: () => true,
95
- validatorsForType: () => ({
96
- description: ['string'],
97
- defaultDescription: ['string']
98
- }),
99
- validate: (data) => data,
100
- formalize: (schema) => schema
101
- })]
102
- });
105
+ options: async (schema, options = { }) => {
106
+
107
+ options.command = options.command || command;
108
+
109
+ schema = formalize(schema);
103
110
 
104
111
  let nonOptions;
105
112
  let nonOptionsIndex = args.indexOf('--');
@@ -115,9 +122,9 @@ const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placehol
115
122
 
116
123
  const printHelp = (error) => {
117
124
 
118
- if (typeof options?.cmd?.description === 'string') {
125
+ if (typeof options?.command?.description === 'string') {
119
126
  print();
120
- print(options.cmd.description);
127
+ print(options.command.description);
121
128
  }
122
129
 
123
130
  print();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/arguments-parser",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Yet another arguments parser.",
5
5
  "main": "index.js",
6
6
  "type": "module",