Repository navigation
Expand file tree
/
Copy pathapi.go
More file actions
925 lines (639 loc) · 22 KB
/
Copy pathapi.go
File metadata and controls
925 lines (639 loc) · 22 KB
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
// Copyright 2019-2025 Matthew Wilson and Synesis Information Systems.
// Copyright 2015-2019 Matthew Wilson. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
/*
* Created: 15th August 2015
* Updated: 1st March 2025
*/
package clasp
import (
"fmt"
"path"
"strings"
)
/* /////////////////////////////////////////////////////////////////////////
* types
*/
type ParseFlag int
/* /////////////////////////////////////////////////////////////////////////
* constants
*/
const (
Parse_None ParseFlag = 0
)
const (
Parse_TreatSingleHyphenAsValue ParseFlag = 1 << iota // Causes a single hyphen command-line argument - `"-"` - to be treated as a value. This is commonly used to indicate reading/writing from/to standard input/output streams.
Parse_DontRecogniseDoubleHyphenToStartValues // Recognises a double hyphen command-line argument - `"--"` - as an instruction to treat all remaining arguments as values.
Parse_DontMergeBitFlagsIntoBitFlags64 // Suppresses the default behaviour to mix into the `int64` result matched `int` bitFlagss (see [Specification.SetBitFlags]) when no matched `int64` bitFlagss (see [Specification.SetBitFlags64]) are specified.
Parse_DontMarkUsedDuringParseWhenMatchingBitFlags // Suppresses the default behaviour to mark as used (see [Argument.Use]) flags that have been provided receiver variables in [Specification.SetBitFlags] or [Specification.SetBitFlags64].
)
const (
ParseTreatSingleHyphenAsValue = Parse_TreatSingleHyphenAsValue // Deprecated: Instead use [Parse_TreatSingleHyphenAsValue].
ParseDontRecogniseDoubleHyphenToStartValues = Parse_DontRecogniseDoubleHyphenToStartValues // Deprecated: Instead use [Parse_DontRecogniseDoubleHyphenToStartValues].
)
/* /////////////////////////////////////////////////////////////////////////
* types
*/
// Enumeration type that defines the nature of arguments.
type ArgType int
const (
FlagType ArgType = 1 // Defines a flag.
OptionType ArgType = 2 // Defines an option.
ValueType ArgType = 3 // Defines a value.
SectionType ArgType = 21 // Defines a section.
optionViaAlias ArgType = -98
int_1_ ArgType = -99
)
// Structure that defines a specification for an argument.
type Specification struct {
Type ArgType
Name string
Aliases []string
Help string
ValueSet []string
BitFlags int
BitFlags64 int64
Extras map[string]any
flagsReceiver *int
flags64Receiver *int64
}
// Structure that defines a parsed argument.
type Argument struct {
ResolvedName string
GivenName string
Value string
Type ArgType
CmdLineIndex int
NumGivenHyphens int
ArgumentSpecification *Specification
Flags int
hasBeenUsed int
}
// Structure that defines result of parsing (see [Parse]).
type Arguments struct {
Arguments []*Argument // Array of all arguments.
Flags []*Argument // Array of all flags.
Options []*Argument // Array of all options.
Values []*Argument // Array of all values.
Argv []string // The original argument string array passed to [Parse].
ProgramName string // The program name.
specifications []*Specification
bitFlags int
bitFlags64 int64
}
// Structure that defines parse options (see [Parse]).
type ParseParams struct {
Specifications []Specification
Flags ParseFlag
}
// Obtains, by value, a specification containing a stock specification of a '--help' flag.
func HelpFlag() Specification {
// TODO: reimplement in terms of [Flag] ??
return Specification{FlagType, "--help", nil, "Shows this help and exits", nil, 0, 0, nil, nil, nil}
}
// Obtains, by value, a specification containing a stock specification of a '--version' flag.
func VersionFlag() Specification {
// TODO: reimplement in terms of [Flag] ??
return Specification{FlagType, "--version", nil, "Shows version information and exits", nil, 0, 0, nil, nil, nil}
}
func (at ArgType) String() string {
switch at {
case FlagType:
return "Flag"
case OptionType, optionViaAlias:
return "Option"
case ValueType:
return "Value"
case SectionType:
return "Section"
default:
return fmt.Sprintf("<%T %d>", at, at)
}
}
func valueSetString(vs []string) string {
elems := make([]string, len(vs))
for ix, elem := range vs {
elems[ix] = fmt.Sprintf(`"%s"`, elem)
}
return "[" + strings.Join(elems, ", ") + "]"
}
func (specification Specification) String() string {
switch specification.Type {
case FlagType:
return fmt.Sprintf("<%T{ Type=%v, Name=%q, Aliases=%v, Help=%q, BitFlags=0x%x, flagsReceiver=%p, BitFlags64=0x%x, flags64Receiver=%p, Extras=%v }>", specification, specification.Type, specification.Name, specification.Aliases, specification.Help, specification.BitFlags, specification.flagsReceiver, specification.BitFlags64, specification.flags64Receiver, specification.Extras)
case OptionType, optionViaAlias:
return fmt.Sprintf("<%T{ Type=%v, Name=%q, Aliases=%v, Help=%q, ValueSet=%v, Extras=%v }>", specification, specification.Type, specification.Name, specification.Aliases, specification.Help, valueSetString(specification.ValueSet), specification.Extras)
case ValueType:
return fmt.Sprintf("<%T{ Type=%v, Extras=%v }>", specification, specification.Type, specification.Extras)
case SectionType:
return fmt.Sprintf("<%T{ Type=%v, Name=%q }>", specification, specification.Type, specification.Name)
default:
return fmt.Sprintf("<%T{ Type=%v, Name=%q, Aliases=%v, Help=%q, ValueSet=%v, BitFlags=0x%x, BitFlags64=0x%x, Extras=%v }>", specification, specification.Type, specification.Name, specification.Aliases, specification.Help, valueSetString(specification.ValueSet), specification.BitFlags, specification.BitFlags64, specification.Extras)
}
}
func (argument Argument) String() string {
return fmt.Sprintf("<%T{ ResolvedName=%q, GivenName=%q, Value=%q, Type=%v, CmdLineIndex=%d, NumGivenHyphens=%d, ArgumentSpecification=%v, Flags=0x%x, used=%t }>", argument, argument.ResolvedName, argument.GivenName, argument.Value, argument.Type, argument.CmdLineIndex, argument.NumGivenHyphens, argument.ArgumentSpecification, argument.Flags, argument.hasBeenUsed != 0)
}
func (arguments Arguments) String() string {
return fmt.Sprintf("<%T{ Arguments=%v, Flags=%v, Options=%v, Values=%v, Argv=%v, ProgramName=%q }>", arguments, arguments.Arguments, arguments.Flags, arguments.Options, arguments.Values, arguments.Argv, arguments.ProgramName)
}
func (params ParseParams) String() string {
return fmt.Sprintf("<%T{ Specifications=%v, Flags=0x%x }>", params, params.Specifications, params.Flags)
}
/* builders */
// Creates a flag specification, with the given name.
func Flag(name string) (result Specification) {
result.Type = FlagType
result.Name = name
return
}
// Creates an option specification, with the given name.
func Option(name string) (result Specification) {
result.Type = OptionType
result.Name = name
return
}
// Creates a section specification, with the given name.
func Section(name string) (result Specification) {
result.Type = SectionType
result.Name = name
return
}
// Creates a specification for an actual flag/option, with 1 or more aliases.
func AliasesFor(actual string, alias0 string, other_aliases ...string) (result Specification) {
result.Type = FlagType
result.Name = actual
result.Aliases = append([]string{alias0}, other_aliases...)
return
}
// Builder method that specifies bit flag(s) ([Specification.BitFlags]) and,
// optionally, a flags receiver variable to be associated with the
// specification. If a flags receiver variable is given then a matching
// [Argument] will be marked as used automationally during parsing
// ([Parse]).
func (specification Specification) SetBitFlags(bitFlags int, flagsReceiver *int) (result Specification) {
specification.BitFlags = bitFlags
specification.flagsReceiver = flagsReceiver
return specification
}
// Builder method that specifies bit flag(s) ([Specification.BitFlags64])
// and, optionally, a flags receiver variable to be associated with the
// specification. If a flags receiver variable is given then a matching
// [Argument] will be marked as used automationally during parsing
// ([Parse]).
//
// NOTE: This is meaningful only to specifications that describes [Type] is
// [FlagType]. A future version may issue a panic if called on another
// argument type.
func (specification Specification) SetBitFlags64(bitFlags int64, flagsReceiver *int64) (result Specification) {
specification.BitFlags64 = bitFlags
specification.flags64Receiver = flagsReceiver
return specification
}
// Builder method to set the help string for a specification.
func (specification Specification) SetHelp(help string) Specification {
specification.Help = help
return specification
}
// Builder method to set the values for an option specification.
func (specification Specification) SetValues(values ...string) Specification {
specification.ValueSet = values
return specification
}
// Builder method that sets the alias.
func (specification Specification) SetAlias(s string) Specification {
specification.Aliases = []string{s}
return specification
}
// Builder method that sets one or more aliases.
func (specification Specification) SetAliases(aliases ...string) Specification {
specification.Aliases = aliases
return specification
}
// Builder method to set an Extras entry.
func (specification Specification) SetExtra(key string, value any) Specification {
if specification.Extras == nil {
specification.Extras = make(map[string]any)
}
specification.Extras[key] = value
return specification
}
// Builder method to denote the end of building. All uses of building should
// be using this method, as future versions may change the types/semantics
// of the other builder methods.
func (specification Specification) End() Specification {
return specification
}
/* /////////////////////////////////////////////////////////////////////////
* helpers
*/
func (params *ParseParams) findSpecification(name string) (found bool, specification *Specification, specificationIndex int) {
// Algorithm:
//
// 1. search for specification with that name
// 2. search for specification with that alias
// 3. return nil
for i, spec := range params.Specifications {
if name == spec.Name {
return true, &spec, i
}
}
for i, spec := range params.Specifications {
for _, n := range spec.Aliases {
if name == n {
return true, &spec, i
}
}
}
return false, nil, -1
}
/* /////////////////////////////////////////////////////////////////////////
* API
*/
// Marks an argument as used, such that it will not be obtained in a call to
// [Arguments.GetUnusedFlags] / [Arguments.GetUnusedOptions] /
// [Arguments.GetUnusedFlagsAndOptions].
//
// NOTE: This is meaningful only to arguments whose [Argument.Type] is
// [FlagType] or [OptionType]. A future version may issue a panic if called
// on another argument type.
func (arg *Argument) Use() {
// TODO: switch on `FlagType` / `OptionType` and warn in other cases
arg.hasBeenUsed = 1
}
func (arg Argument) isUnused() bool {
return 0 == arg.hasBeenUsed
}
// T.B.C.
func (arg Argument) Str() string {
switch arg.Type {
case OptionType:
return fmt.Sprintf("%s=%s", arg.ResolvedName, arg.Value)
default:
return arg.ResolvedName
}
}
// T.B.C.
func Parse(argv []string, params ParseParams) *Arguments {
args := new(Arguments)
args.Arguments = make([]*Argument, 0)
args.Flags = make([]*Argument, 0)
args.Options = make([]*Argument, 0)
args.Values = make([]*Argument, 0)
args.Argv = argv
if len(argv) > 0 {
args.ProgramName = path.Base(argv[0])
} else {
args.ProgramName = ""
}
treatingAsValues := false
nextIsOptValue := false
if len(argv) > 0 {
for i, s := range argv[1:] {
if !treatingAsValues && "--" == s && (0 == (params.Flags & Parse_DontRecogniseDoubleHyphenToStartValues)) {
treatingAsValues = true
continue
}
if nextIsOptValue {
nextIsOptValue = false
args.Arguments[len(args.Arguments)-1].Value = s
continue
}
arg := new(Argument)
arg.CmdLineIndex = i + 1
arg.Flags = int(params.Flags)
arg.ArgumentSpecification = nil
numHyphens := 0
isSingle := false
if !treatingAsValues {
l := len(s)
if 1 == l && "-" == s {
numHyphens = 1
isSingle = true
} else if 2 == l && "--" == s {
numHyphens = 2
} else {
numHyphens = strings.IndexFunc(s, func(c rune) bool { return '-' != c })
}
}
arg.NumGivenHyphens = numHyphens
switch numHyphens {
case 0:
arg.Type = ValueType
arg.Value = s
default:
nv := strings.SplitN(s, "=", 2)
if len(nv) > 1 {
arg.Type = OptionType
arg.GivenName = nv[0]
arg.ResolvedName = nv[0]
arg.Value = nv[1]
if found, specification, _ := params.findSpecification(arg.ResolvedName); found {
arg.ResolvedName = specification.Name
arg.ArgumentSpecification = specification
} else {
}
} else {
// Here we have to be flexible, and examine
// whether the apparent flag is, in fact, an
// option
resolvedName := s
argType := FlagType
if found, specification, _ := params.findSpecification(s); found {
resolvedName = specification.Name
argType = specification.Type
arg.ArgumentSpecification = specification
if ix_equals := strings.Index(resolvedName, "="); ix_equals >= 0 {
res_nm := resolvedName[:ix_equals]
value := resolvedName[ix_equals+1:]
argType = optionViaAlias
s = resolvedName
resolvedName = res_nm
arg.Value = value
// Now need to look up the actual underlying specification
if actualFound, actualSpecification, _ := params.findSpecification(res_nm); actualFound {
arg.ArgumentSpecification = actualSpecification
}
}
} else {
// Now we test to see whether every character yields
// a specification. If so, we convert all, add them in, then
// skip to the next input
validCompoundFlag := len(s) > 1
compoundArguments := make([]*Argument, 0, len(s)-1)
for j, c := range s {
if 0 == j {
continue
}
testAlias := fmt.Sprintf("-%c", c)
if compoundFound, compoundSpec, _ := params.findSpecification(testAlias); compoundFound && compoundSpec.Type == FlagType {
var compoundArg Argument
compoundArg.ResolvedName = compoundSpec.Name
compoundArg.GivenName = s
compoundArg.Value = ""
compoundArg.Type = FlagType
compoundArg.CmdLineIndex = arg.CmdLineIndex
compoundArg.ArgumentSpecification = compoundSpec
compoundArg.Flags = arg.Flags
if ix_equals := strings.Index(compoundArg.ResolvedName, "="); ix_equals >= 0 {
res_nm := compoundArg.ResolvedName[:ix_equals]
value := compoundArg.ResolvedName[ix_equals+1:]
compoundArg.Type = OptionType
s = compoundArg.ResolvedName
compoundArg.ResolvedName = res_nm
compoundArg.Value = value
// Now need to look up the actual underlying specification
if actualFound, actualSpecification, _ := params.findSpecification(res_nm); actualFound {
compoundArg.ArgumentSpecification = actualSpecification
}
}
compoundArguments = append(compoundArguments, &compoundArg)
} else {
validCompoundFlag = false
break
}
}
if validCompoundFlag {
args.Arguments = append(args.Arguments, compoundArguments...)
continue
}
}
switch argType {
case optionViaAlias:
arg.Type = OptionType
default:
arg.Type = argType
}
arg.GivenName = s
arg.ResolvedName = resolvedName
if OptionType == arg.Type {
if optionViaAlias != argType {
nextIsOptValue = true
}
} else {
if isSingle && (0 != (params.Flags & ParseTreatSingleHyphenAsValue)) {
arg.Type = ValueType
arg.Value = s
}
}
}
}
args.Arguments = append(args.Arguments, arg)
}
}
for _, arg := range args.Arguments {
switch arg.Type {
case FlagType:
args.Flags = append(args.Flags, arg)
case OptionType:
args.Options = append(args.Options, arg)
case ValueType:
args.Values = append(args.Values, arg)
}
}
args.specifications = make([]*Specification, len(params.Specifications))
for i, spec := range params.Specifications {
var p *Specification = new(Specification)
*p = spec
args.specifications[i] = p
}
// now process the bit flags
{
for _, arg := range args.Flags {
spec := arg.ArgumentSpecification
if nil != spec {
if 0 != spec.BitFlags64 {
if nil != spec.flags64Receiver {
*spec.flags64Receiver |= spec.BitFlags64
if 0 == (Parse_DontMarkUsedDuringParseWhenMatchingBitFlags & params.Flags) {
arg.Use()
}
}
args.bitFlags64 |= spec.BitFlags64
} else {
if 0 != spec.BitFlags {
if nil != spec.flagsReceiver {
*spec.flagsReceiver |= spec.BitFlags
if 0 == (Parse_DontMarkUsedDuringParseWhenMatchingBitFlags & params.Flags) {
arg.Use()
}
}
args.bitFlags |= spec.BitFlags
if 0 == (Parse_DontMergeBitFlagsIntoBitFlags64 & params.Flags) {
if nil != spec.flags64Receiver {
*spec.flags64Receiver |= spec.BitFlags64
if 0 == (Parse_DontMarkUsedDuringParseWhenMatchingBitFlags & params.Flags) {
arg.Use()
}
}
args.bitFlags64 |= int64(spec.BitFlags)
}
}
}
}
}
}
return args
}
// Obtains the combined bit-flags of all flag arguments with associated
// [Specification.BitFlags] that are observed during parsing.
func (args Arguments) AllBitFlags() int {
return args.bitFlags
}
// Obtains the combined bit-flags of all flag arguments with associated
// [Specification.BitFlags64] that are observed during parsing, and, unless
// [Parse_DontMergeBitFlagsIntoBitFlags64] was specified to [Parse],
// combined with any occurrences of arguments associated with
// [Specification.BitFlags].
func (args Arguments) AllBit64Flags() int64 {
return args.bitFlags64
}
// Indicates whether the given argument - specified either as `string` or
// [Specification] - was observed during parsing.
//
// If an argument is found, then it is marked used.
func (args *Arguments) FlagIsSpecified(id any) bool {
name := ""
found := false
if s, is_string := id.(string); is_string {
name = s
found = true
}
if spec, is_Specification := id.(Specification); is_Specification {
switch spec.Type {
case OptionType:
// TODO: issue warning
fallthrough
case FlagType:
name = spec.Name
found = true
default:
panic(fmt.Sprintf("invoked FlagIsSpecified() passing a non-Flag (and non-Option) Specification '%v'", spec))
}
}
if !found && nil != id {
panic(fmt.Sprintf("invoked FlagIsSpecified() passing a value - '%v' - that is neither string nor specification", id))
}
for i, f := range args.Flags {
// TODO: mark as used
_ = i
if name == f.ResolvedName {
f.Use()
return true
}
}
return false
}
// Looks for given flag argument - specified either as `string` or
// [Specification] - in the parsed arguments.
//
// If an argument is found, then it is marked used.
func (args *Arguments) LookupFlag(id any) (*Argument, bool) {
name := ""
found := false
if s, is_string := id.(string); is_string {
name = s
found = true
}
if spec, is_Specification := id.(Specification); is_Specification {
switch spec.Type {
case FlagType:
name = spec.Name
found = true
default:
panic(fmt.Sprintf("invoked LookupFlag() passing a non-Flag Specification '%v'", spec))
}
}
if !found && nil != id {
panic(fmt.Sprintf("invoked LookupFlag() passing a value - '%v' - that is neither string nor specification", id))
}
for i, o := range args.Flags {
// TODO: mark as used
_ = i
if name == o.ResolvedName {
o.Use()
return o, true
}
}
return nil, false
}
// Looks for given option argument - specified either as `string` or
// [Specification] - in the parsed arguments.
//
// If an argument is found, then it is marked used.
func (args *Arguments) LookupOption(id any) (*Argument, bool) {
name := ""
found := false
if s, is_string := id.(string); is_string {
name = s
found = true
}
if spec, is_Specification := id.(Specification); is_Specification {
switch spec.Type {
case OptionType:
name = spec.Name
found = true
default:
panic(fmt.Sprintf("invoked LookupOption() passing a non-Option Specification '%v'", spec))
}
}
if !found && nil != id {
panic(fmt.Sprintf("invoked LookupOption() passing a value - '%v' - that is neither string nor specification", id))
}
for i, o := range args.Options {
// TODO: mark as used
_ = i
if name == o.ResolvedName {
o.Use()
return o, true
}
}
return nil, false
}
// Obtains a sequence of all unused flag arguments.
func (args *Arguments) GetUnusedFlags() []*Argument {
var unused []*Argument
for _, f := range args.Flags {
if f.isUnused() {
unused = append(unused, f)
}
}
return unused
}
// Obtains a sequence of all unused option arguments.
func (args *Arguments) GetUnusedOptions() []*Argument {
var unused []*Argument
for _, o := range args.Options {
if o.isUnused() {
unused = append(unused, o)
}
}
return unused
}
// Obtains a sequence of all unused flag and option arguments.
func (args *Arguments) GetUnusedFlagsAndOptions() []*Argument {
var unused []*Argument
for _, arg := range args.Arguments {
switch arg.Type {
case FlagType:
fallthrough
case OptionType:
if arg.isUnused() {
unused = append(unused, arg)
}
break
}
}
return unused
}
// Simple helper function to convert a variable number of alias string
// arguments into a string array.
func Aliases(aliases ...string) []string {
r := make([]string, len(aliases))
for i, alias := range aliases {
r[i] = alias
}
return r
}
/* ///////////////////////////// end of file //////////////////////////// */