forked from nodeca/argparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
argparse.js
3707 lines (3241 loc) · 127 KB
/
argparse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Port of python's argparse module, version 3.9.0:
// https://github.com/python/cpython/blob/v3.9.0rc1/Lib/argparse.py
'use strict'
// Copyright (C) 2010-2020 Python Software Foundation.
// Copyright (C) 2020 argparse.js authors
/*
* Command-line parsing library
*
* This module is an optparse-inspired command-line parsing library that:
*
* - handles both optional and positional arguments
* - produces highly informative usage messages
* - supports parsers that dispatch to sub-parsers
*
* The following is a simple usage example that sums integers from the
* command-line and writes the result to a file::
*
* parser = argparse.ArgumentParser(
* description='sum the integers at the command line')
* parser.add_argument(
* 'integers', metavar='int', nargs='+', type=int,
* help='an integer to be summed')
* parser.add_argument(
* '--log', default=sys.stdout, type=argparse.FileType('w'),
* help='the file where the sum should be written')
* args = parser.parse_args()
* args.log.write('%s' % sum(args.integers))
* args.log.close()
*
* The module contains the following public classes:
*
* - ArgumentParser -- The main entry point for command-line parsing. As the
* example above shows, the add_argument() method is used to populate
* the parser with actions for optional and positional arguments. Then
* the parse_args() method is invoked to convert the args at the
* command-line into an object with attributes.
*
* - ArgumentError -- The exception raised by ArgumentParser objects when
* there are errors with the parser's actions. Errors raised while
* parsing the command-line are caught by ArgumentParser and emitted
* as command-line messages.
*
* - FileType -- A factory for defining types of files to be created. As the
* example above shows, instances of FileType are typically passed as
* the type= argument of add_argument() calls.
*
* - Action -- The base class for parser actions. Typically actions are
* selected by passing strings like 'store_true' or 'append_const' to
* the action= argument of add_argument(). However, for greater
* customization of ArgumentParser actions, subclasses of Action may
* be defined and passed as the action= argument.
*
* - HelpFormatter, RawDescriptionHelpFormatter, RawTextHelpFormatter,
* ArgumentDefaultsHelpFormatter -- Formatter classes which
* may be passed as the formatter_class= argument to the
* ArgumentParser constructor. HelpFormatter is the default,
* RawDescriptionHelpFormatter and RawTextHelpFormatter tell the parser
* not to change the formatting for help text, and
* ArgumentDefaultsHelpFormatter adds information about argument defaults
* to the help.
*
* All other classes in this module are considered implementation details.
* (Also note that HelpFormatter and RawDescriptionHelpFormatter are only
* considered public as object names -- the API of the formatter objects is
* still considered an implementation detail.)
*/
const SUPPRESS = '==SUPPRESS=='
const OPTIONAL = '?'
const ZERO_OR_MORE = '*'
const ONE_OR_MORE = '+'
const PARSER = 'A...'
const REMAINDER = '...'
const _UNRECOGNIZED_ARGS_ATTR = '_unrecognized_args'
// ==================================
// Utility functions used for porting
// ==================================
const assert = require('assert')
const util = require('util')
const fs = require('fs')
const sub = require('./lib/sub')
const path = require('path')
const repr = util.inspect
function get_argv() {
// omit first argument (which is assumed to be interpreter - `node`, `coffee`, `ts-node`, etc.)
return process.argv.slice(1)
}
function get_terminal_size() {
return {
columns: +process.env.COLUMNS || process.stdout.columns || 80
}
}
function hasattr(object, name) {
return Object.prototype.hasOwnProperty.call(object, name)
}
function getattr(object, name, value) {
return hasattr(object, name) ? object[name] : value
}
function setattr(object, name, value) {
object[name] = value
}
function setdefault(object, name, value) {
if (!hasattr(object, name)) object[name] = value
return object[name]
}
function delattr(object, name) {
delete object[name]
}
function range(from, to, step=1) {
// range(10) is equivalent to range(0, 10)
if (arguments.length === 1) [ to, from ] = [ from, 0 ]
if (typeof from !== 'number' || typeof to !== 'number' || typeof step !== 'number') {
throw new TypeError('argument cannot be interpreted as an integer')
}
if (step === 0) throw new TypeError('range() arg 3 must not be zero')
let result = []
if (step > 0) {
for (let i = from; i < to; i += step) result.push(i)
} else {
for (let i = from; i > to; i += step) result.push(i)
}
return result
}
function splitlines(str, keepends = false) {
let result
if (!keepends) {
result = str.split(/\r\n|[\n\r\v\f\x1c\x1d\x1e\x85\u2028\u2029]/)
} else {
result = []
let parts = str.split(/(\r\n|[\n\r\v\f\x1c\x1d\x1e\x85\u2028\u2029])/)
for (let i = 0; i < parts.length; i += 2) {
result.push(parts[i] + (i + 1 < parts.length ? parts[i + 1] : ''))
}
}
if (!result[result.length - 1]) result.pop()
return result
}
function _string_lstrip(string, prefix_chars) {
let idx = 0
while (idx < string.length && prefix_chars.includes(string[idx])) idx++
return idx ? string.slice(idx) : string
}
function _string_split(string, sep, maxsplit) {
let result = string.split(sep)
if (result.length > maxsplit) {
result = result.slice(0, maxsplit).concat([ result.slice(maxsplit).join(sep) ])
}
return result
}
function _array_equal(array1, array2) {
if (array1.length !== array2.length) return false
for (let i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) return false
}
return true
}
function _array_remove(array, item) {
let idx = array.indexOf(item)
if (idx === -1) throw new TypeError(sub('%r not in list', item))
array.splice(idx, 1)
}
// normalize choices to array;
// this isn't required in python because `in` and `map` operators work with anything,
// but in js dealing with multiple types here is too clunky
function _choices_to_array(choices) {
if (choices === undefined) {
return []
} else if (Array.isArray(choices)) {
return choices
} else if (choices !== null && typeof choices[Symbol.iterator] === 'function') {
return Array.from(choices)
} else if (typeof choices === 'object' && choices !== null) {
return Object.keys(choices)
} else {
throw new Error(sub('invalid choices value: %r', choices))
}
}
// decorator that allows a class to be called without new
function _callable(cls) {
let result = { // object is needed for inferred class name
[cls.name]: function (...args) {
let this_class = new.target === result || !new.target
return Reflect.construct(cls, args, this_class ? cls : new.target)
}
}
result[cls.name].prototype = cls.prototype
// fix default tag for toString, e.g. [object Action] instead of [object Object]
cls.prototype[Symbol.toStringTag] = cls.name
return result[cls.name]
}
function _alias(object, from, to) {
try {
let name = object.constructor.name
Object.defineProperty(object, from, {
value: util.deprecate(object[to], sub('%s.%s() is renamed to %s.%s()',
name, from, name, to)),
enumerable: false
})
} catch {}
}
// decorator that allows snake_case class methods to be called with camelCase and vice versa
function _camelcase_alias(_class) {
for (let name of Object.getOwnPropertyNames(_class.prototype)) {
let camelcase = name.replace(/\w_[a-z]/g, s => s[0] + s[2].toUpperCase())
if (camelcase !== name) _alias(_class.prototype, camelcase, name)
}
return _class
}
function _to_legacy_name(key) {
key = key.replace(/\w_[a-z]/g, s => s[0] + s[2].toUpperCase())
if (key === 'default') key = 'defaultValue'
if (key === 'const') key = 'constant'
return key
}
function _to_new_name(key) {
if (key === 'defaultValue') key = 'default'
if (key === 'constant') key = 'const'
key = key.replace(/[A-Z]/g, c => '_' + c.toLowerCase())
return key
}
// parse options
let no_default = Symbol('no_default_value')
function _parse_opts(args, descriptor) {
function get_name() {
let stack = new Error().stack.split('\n')
.map(x => x.match(/^ at (.*) \(.*\)$/))
.filter(Boolean)
.map(m => m[1])
.map(fn => fn.match(/[^ .]*$/)[0])
if (stack.length && stack[0] === get_name.name) stack.shift()
if (stack.length && stack[0] === _parse_opts.name) stack.shift()
return stack.length ? stack[0] : ''
}
args = Array.from(args)
let kwargs = {}
let result = []
let last_opt = args.length && args[args.length - 1]
if (typeof last_opt === 'object' && last_opt !== null && !Array.isArray(last_opt) &&
(!last_opt.constructor || last_opt.constructor.name === 'Object')) {
kwargs = Object.assign({}, args.pop())
}
// LEGACY (v1 compatibility): camelcase
let renames = []
for (let key of Object.keys(descriptor)) {
let old_name = _to_legacy_name(key)
if (old_name !== key && (old_name in kwargs)) {
if (key in kwargs) {
// default and defaultValue specified at the same time, happens often in old tests
//throw new TypeError(sub('%s() got multiple values for argument %r', get_name(), key))
} else {
kwargs[key] = kwargs[old_name]
}
renames.push([ old_name, key ])
delete kwargs[old_name]
}
}
if (renames.length) {
let name = get_name()
deprecate('camelcase_' + name, sub('%s(): following options are renamed: %s',
name, renames.map(([ a, b ]) => sub('%r -> %r', a, b))))
}
// end
let missing_positionals = []
let positional_count = args.length
for (let [ key, def ] of Object.entries(descriptor)) {
if (key[0] === '*') {
if (key.length > 0 && key[1] === '*') {
// LEGACY (v1 compatibility): camelcase
let renames = []
for (let key of Object.keys(kwargs)) {
let new_name = _to_new_name(key)
if (new_name !== key && (key in kwargs)) {
if (new_name in kwargs) {
// default and defaultValue specified at the same time, happens often in old tests
//throw new TypeError(sub('%s() got multiple values for argument %r', get_name(), new_name))
} else {
kwargs[new_name] = kwargs[key]
}
renames.push([ key, new_name ])
delete kwargs[key]
}
}
if (renames.length) {
let name = get_name()
deprecate('camelcase_' + name, sub('%s(): following options are renamed: %s',
name, renames.map(([ a, b ]) => sub('%r -> %r', a, b))))
}
// end
result.push(kwargs)
kwargs = {}
} else {
result.push(args)
args = []
}
} else if (key in kwargs && args.length > 0) {
throw new TypeError(sub('%s() got multiple values for argument %r', get_name(), key))
} else if (key in kwargs) {
result.push(kwargs[key])
delete kwargs[key]
} else if (args.length > 0) {
result.push(args.shift())
} else if (def !== no_default) {
result.push(def)
} else {
missing_positionals.push(key)
}
}
if (Object.keys(kwargs).length) {
throw new TypeError(sub('%s() got an unexpected keyword argument %r',
get_name(), Object.keys(kwargs)[0]))
}
if (args.length) {
let from = Object.entries(descriptor).filter(([ k, v ]) => k[0] !== '*' && v !== no_default).length
let to = Object.entries(descriptor).filter(([ k ]) => k[0] !== '*').length
throw new TypeError(sub('%s() takes %s positional argument%s but %s %s given',
get_name(),
from === to ? sub('from %s to %s', from, to) : to,
from === to && to === 1 ? '' : 's',
positional_count,
positional_count === 1 ? 'was' : 'were'))
}
if (missing_positionals.length) {
let strs = missing_positionals.map(repr)
if (strs.length > 1) strs[strs.length - 1] = 'and ' + strs[strs.length - 1]
let str_joined = strs.join(strs.length === 2 ? '' : ', ')
throw new TypeError(sub('%s() missing %i required positional argument%s: %s',
get_name(), strs.length, strs.length === 1 ? '' : 's', str_joined))
}
return result
}
let _deprecations = {}
function deprecate(id, string) {
_deprecations[id] = _deprecations[id] || util.deprecate(() => {}, string)
_deprecations[id]()
}
// =============================
// Utility functions and classes
// =============================
function _AttributeHolder(cls = Object) {
/*
* Abstract base class that provides __repr__.
*
* The __repr__ method returns a string in the format::
* ClassName(attr=name, attr=name, ...)
* The attributes are determined either by a class-level attribute,
* '_kwarg_names', or by inspecting the instance __dict__.
*/
return class _AttributeHolder extends cls {
[util.inspect.custom]() {
let type_name = this.constructor.name
let arg_strings = []
let star_args = {}
for (let arg of this._get_args()) {
arg_strings.push(repr(arg))
}
for (let [ name, value ] of this._get_kwargs()) {
if (/^[a-z_][a-z0-9_$]*$/i.test(name)) {
arg_strings.push(sub('%s=%r', name, value))
} else {
star_args[name] = value
}
}
if (Object.keys(star_args).length) {
arg_strings.push(sub('**%s', repr(star_args)))
}
return sub('%s(%s)', type_name, arg_strings.join(', '))
}
toString() {
return this[util.inspect.custom]()
}
_get_kwargs() {
return Object.entries(this)
}
_get_args() {
return []
}
}
}
function _copy_items(items) {
if (items === undefined) {
return []
}
return items.slice(0)
}
// ===============
// Formatting Help
// ===============
const HelpFormatter = _camelcase_alias(_callable(class HelpFormatter {
/*
* Formatter for generating usage messages and argument help strings.
*
* Only the name of this class is considered a public API. All the methods
* provided by the class are considered an implementation detail.
*/
constructor() {
let [
prog,
indent_increment,
max_help_position,
width
] = _parse_opts(arguments, {
prog: no_default,
indent_increment: 2,
max_help_position: 24,
width: undefined
})
// default setting for width
if (width === undefined) {
width = get_terminal_size().columns
width -= 2
}
this._prog = prog
this._indent_increment = indent_increment
this._max_help_position = Math.min(max_help_position,
Math.max(width - 20, indent_increment * 2))
this._width = width
this._current_indent = 0
this._level = 0
this._action_max_length = 0
this._root_section = this._Section(this, undefined)
this._current_section = this._root_section
this._whitespace_matcher = /[ \t\n\r\f\v]+/g // equivalent to python /\s+/ with ASCII flag
this._long_break_matcher = /\n\n\n+/g
}
// ===============================
// Section and indentation methods
// ===============================
_indent() {
this._current_indent += this._indent_increment
this._level += 1
}
_dedent() {
this._current_indent -= this._indent_increment
assert(this._current_indent >= 0, 'Indent decreased below 0.')
this._level -= 1
}
_add_item(func, args) {
this._current_section.items.push([ func, args ])
}
// ========================
// Message building methods
// ========================
start_section(heading) {
this._indent()
let section = this._Section(this, this._current_section, heading)
this._add_item(section.format_help.bind(section), [])
this._current_section = section
}
end_section() {
this._current_section = this._current_section.parent
this._dedent()
}
add_text(text) {
if (text !== SUPPRESS && text !== undefined) {
this._add_item(this._format_text.bind(this), [text])
}
}
add_usage(usage, actions, groups, prefix = undefined) {
if (usage !== SUPPRESS) {
let args = [ usage, actions, groups, prefix ]
this._add_item(this._format_usage.bind(this), args)
}
}
add_argument(action) {
if (action.help !== SUPPRESS) {
// find all invocations
let invocations = [this._format_action_invocation(action)]
for (let subaction of this._iter_indented_subactions(action)) {
invocations.push(this._format_action_invocation(subaction))
}
// update the maximum item length
let invocation_length = Math.max(...invocations.map(invocation => invocation.length))
let action_length = invocation_length + this._current_indent
this._action_max_length = Math.max(this._action_max_length,
action_length)
// add the item to the list
this._add_item(this._format_action.bind(this), [action])
}
}
add_arguments(actions) {
for (let action of actions) {
this.add_argument(action)
}
}
// =======================
// Help-formatting methods
// =======================
format_help() {
let help = this._root_section.format_help()
if (help) {
help = help.replace(this._long_break_matcher, '\n\n')
help = help.replace(/^\n+|\n+$/g, '') + '\n'
}
return help
}
_join_parts(part_strings) {
return part_strings.filter(part => part && part !== SUPPRESS).join('')
}
_format_usage(usage, actions, groups, prefix) {
if (prefix === undefined) {
prefix = 'usage: '
}
// if usage is specified, use that
if (usage !== undefined) {
usage = sub(usage, { prog: this._prog })
// if no optionals or positionals are available, usage is just prog
} else if (usage === undefined && !actions.length) {
usage = sub('%(prog)s', { prog: this._prog })
// if optionals and positionals are available, calculate usage
} else if (usage === undefined) {
let prog = sub('%(prog)s', { prog: this._prog })
// split optionals from positionals
let optionals = []
let positionals = []
for (let action of actions) {
if (action.option_strings.length) {
optionals.push(action)
} else {
positionals.push(action)
}
}
// build full usage string
let action_usage = this._format_actions_usage([].concat(optionals).concat(positionals), groups)
usage = [ prog, action_usage ].map(String).join(' ')
// wrap the usage parts if it's too long
let text_width = this._width - this._current_indent
if (prefix.length + usage.length > text_width) {
// break usage into wrappable parts
let part_regexp = /\(.*?\)+(?=\s|$)|\[.*?\]+(?=\s|$)|\S+/g
let opt_usage = this._format_actions_usage(optionals, groups)
let pos_usage = this._format_actions_usage(positionals, groups)
let opt_parts = opt_usage.match(part_regexp) || []
let pos_parts = pos_usage.match(part_regexp) || []
assert(opt_parts.join(' ') === opt_usage)
assert(pos_parts.join(' ') === pos_usage)
// helper for wrapping lines
let get_lines = (parts, indent, prefix = undefined) => {
let lines = []
let line = []
let line_len
if (prefix !== undefined) {
line_len = prefix.length - 1
} else {
line_len = indent.length - 1
}
for (let part of parts) {
if (line_len + 1 + part.length > text_width && line) {
lines.push(indent + line.join(' '))
line = []
line_len = indent.length - 1
}
line.push(part)
line_len += part.length + 1
}
if (line.length) {
lines.push(indent + line.join(' '))
}
if (prefix !== undefined) {
lines[0] = lines[0].slice(indent.length)
}
return lines
}
let lines
// if prog is short, follow it with optionals or positionals
if (prefix.length + prog.length <= 0.75 * text_width) {
let indent = ' '.repeat(prefix.length + prog.length + 1)
if (opt_parts.length) {
lines = get_lines([prog].concat(opt_parts), indent, prefix)
lines = lines.concat(get_lines(pos_parts, indent))
} else if (pos_parts.length) {
lines = get_lines([prog].concat(pos_parts), indent, prefix)
} else {
lines = [prog]
}
// if prog is long, put it on its own line
} else {
let indent = ' '.repeat(prefix.length)
let parts = [].concat(opt_parts).concat(pos_parts)
lines = get_lines(parts, indent)
if (lines.length > 1) {
lines = []
lines = lines.concat(get_lines(opt_parts, indent))
lines = lines.concat(get_lines(pos_parts, indent))
}
lines = [prog].concat(lines)
}
// join lines into usage
usage = lines.join('\n')
}
}
// prefix with 'usage:'
return sub('%s%s\n\n', prefix, usage)
}
_format_actions_usage(actions, groups) {
// find group indices and identify actions in groups
let group_actions = new Set()
let inserts = {}
for (let group of groups) {
let start = actions.indexOf(group._group_actions[0])
if (start === -1) {
continue
} else {
let end = start + group._group_actions.length
if (_array_equal(actions.slice(start, end), group._group_actions)) {
for (let action of group._group_actions) {
group_actions.add(action)
}
if (!group.required) {
if (start in inserts) {
inserts[start] += ' ['
} else {
inserts[start] = '['
}
if (end in inserts) {
inserts[end] += ']'
} else {
inserts[end] = ']'
}
} else {
if (start in inserts) {
inserts[start] += ' ('
} else {
inserts[start] = '('
}
if (end in inserts) {
inserts[end] += ')'
} else {
inserts[end] = ')'
}
}
for (let i of range(start + 1, end)) {
inserts[i] = '|'
}
}
}
}
// collect all actions format strings
let parts = []
for (let [ i, action ] of Object.entries(actions)) {
// suppressed arguments are marked with None
// remove | separators for suppressed arguments
if (action.help === SUPPRESS) {
parts.push(undefined)
if (inserts[+i] === '|') {
delete inserts[+i]
} else if (inserts[+i + 1] === '|') {
delete inserts[+i + 1]
}
// produce all arg strings
} else if (!action.option_strings.length) {
let default_value = this._get_default_metavar_for_positional(action)
let part = this._format_args(action, default_value)
// if it's in a group, strip the outer []
if (group_actions.has(action)) {
if (part[0] === '[' && part[part.length - 1] === ']') {
part = part.slice(1, -1)
}
}
// add the action string to the list
parts.push(part)
// produce the first way to invoke the option in brackets
} else {
let option_string = action.option_strings[0]
let part
// if the Optional doesn't take a value, format is:
// -s or --long
if (action.nargs === 0) {
part = action.format_usage()
// if the Optional takes a value, format is:
// -s ARGS or --long ARGS
} else {
let default_value = this._get_default_metavar_for_optional(action)
let args_string = this._format_args(action, default_value)
part = sub('%s %s', option_string, args_string)
}
// make it look optional if it's not required or in a group
if (!action.required && !group_actions.has(action)) {
part = sub('[%s]', part)
}
// add the action string to the list
parts.push(part)
}
}
// insert things at the necessary indices
for (let i of Object.keys(inserts).map(Number).sort((a, b) => b - a)) {
parts.splice(+i, 0, inserts[+i])
}
// join all the action items with spaces
let text = parts.filter(Boolean).join(' ')
// clean up separators for mutually exclusive groups
text = text.replace(/([\[(]) /g, '$1')
text = text.replace(/ ([\])])/g, '$1')
text = text.replace(/[\[(] *[\])]/g, '')
text = text.replace(/\(([^|]*)\)/g, '$1', text)
text = text.trim()
// return the text
return text
}
_format_text(text) {
if (text.includes('%(prog)')) {
text = sub(text, { prog: this._prog })
}
let text_width = Math.max(this._width - this._current_indent, 11)
let indent = ' '.repeat(this._current_indent)
return this._fill_text(text, text_width, indent) + '\n\n'
}
_format_action(action) {
// determine the required width and the entry label
let help_position = Math.min(this._action_max_length + 2,
this._max_help_position)
let help_width = Math.max(this._width - help_position, 11)
let action_width = help_position - this._current_indent - 2
let action_header = this._format_action_invocation(action)
let indent_first
// no help; start on same line and add a final newline
if (!action.help) {
let tup = [ this._current_indent, '', action_header ]
action_header = sub('%*s%s\n', ...tup)
// short action name; start on the same line and pad two spaces
} else if (action_header.length <= action_width) {
let tup = [ this._current_indent, '', action_width, action_header ]
action_header = sub('%*s%-*s ', ...tup)
indent_first = 0
// long action name; start on the next line
} else {
let tup = [ this._current_indent, '', action_header ]
action_header = sub('%*s%s\n', ...tup)
indent_first = help_position
}
// collect the pieces of the action help
let parts = [action_header]
// if there was help for the action, add lines of help text
if (action.help) {
let help_text = this._expand_help(action)
let help_lines = this._split_lines(help_text, help_width)
parts.push(sub('%*s%s\n', indent_first, '', help_lines[0]))
for (let line of help_lines.slice(1)) {
parts.push(sub('%*s%s\n', help_position, '', line))
}
// or add a newline if the description doesn't end with one
} else if (!action_header.endsWith('\n')) {
parts.push('\n')
}
// if there are any sub-actions, add their help as well
for (let subaction of this._iter_indented_subactions(action)) {
parts.push(this._format_action(subaction))
}
// return a single string
return this._join_parts(parts)
}
_format_action_invocation(action) {
if (!action.option_strings.length) {
let default_value = this._get_default_metavar_for_positional(action)
let metavar = this._metavar_formatter(action, default_value)(1)[0]
return metavar
} else {
let parts = []
// if the Optional doesn't take a value, format is:
// -s, --long
if (action.nargs === 0) {
parts = parts.concat(action.option_strings)
// if the Optional takes a value, format is:
// -s ARGS, --long ARGS
} else {
let default_value = this._get_default_metavar_for_optional(action)
let args_string = this._format_args(action, default_value)
for (let option_string of action.option_strings) {
parts.push(sub('%s %s', option_string, args_string))
}
}
return parts.join(', ')
}
}
_metavar_formatter(action, default_metavar) {
let result
if (action.metavar !== undefined) {
result = action.metavar
} else if (action.choices !== undefined) {
let choice_strs = _choices_to_array(action.choices).map(String)
result = sub('{%s}', choice_strs.join(','))
} else {
result = default_metavar
}
function format(tuple_size) {
if (Array.isArray(result)) {
return result
} else {
return Array(tuple_size).fill(result)
}
}
return format
}
_format_args(action, default_metavar) {
let get_metavar = this._metavar_formatter(action, default_metavar)
let result
if (action.nargs === undefined) {
result = sub('%s', ...get_metavar(1))
} else if (action.nargs === OPTIONAL) {
result = sub('[%s]', ...get_metavar(1))
} else if (action.nargs === ZERO_OR_MORE) {
let metavar = get_metavar(1)
if (metavar.length === 2) {
result = sub('[%s [%s ...]]', ...metavar)
} else {
result = sub('[%s ...]', ...metavar)
}
} else if (action.nargs === ONE_OR_MORE) {
result = sub('%s [%s ...]', ...get_metavar(2))
} else if (action.nargs === REMAINDER) {
result = '...'
} else if (action.nargs === PARSER) {
result = sub('%s ...', ...get_metavar(1))
} else if (action.nargs === SUPPRESS) {
result = ''
} else {
let formats
try {
formats = range(action.nargs).map(() => '%s')
} catch (err) {
throw new TypeError('invalid nargs value')
}
result = sub(formats.join(' '), ...get_metavar(action.nargs))
}
return result
}
_expand_help(action) {
let params = Object.assign({ prog: this._prog }, action)
for (let name of Object.keys(params)) {
if (params[name] === SUPPRESS) {
delete params[name]
}
}
for (let name of Object.keys(params)) {
if (params[name] && params[name].name) {
params[name] = params[name].name
}
}
if (params.choices !== undefined) {
let choices_str = _choices_to_array(params.choices).map(String).join(', ')
params.choices = choices_str
}
// LEGACY (v1 compatibility): camelcase
for (let key of Object.keys(params)) {
let old_name = _to_legacy_name(key)
if (old_name !== key) {
params[old_name] = params[key]
}
}
// end
return sub(this._get_help_string(action), params)
}
* _iter_indented_subactions(action) {
if (typeof action._get_subactions === 'function') {
this._indent()
yield* action._get_subactions()
this._dedent()
}
}
_split_lines(text, width) {
text = text.replace(this._whitespace_matcher, ' ').trim()
// The textwrap module is used only for formatting help.
// Delay its import for speeding up the common usage of argparse.
let textwrap = require('./lib/textwrap')
return textwrap.wrap(text, { width })
}
_fill_text(text, width, indent) {
text = text.replace(this._whitespace_matcher, ' ').trim()
let textwrap = require('./lib/textwrap')
return textwrap.fill(text, { width,
initial_indent: indent,
subsequent_indent: indent })
}
_get_help_string(action) {
return action.help
}
_get_default_metavar_for_optional(action) {
return action.dest.toUpperCase()
}