File: argparse.in

package info (click to toggle)
fish 3.0.2-2%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 36,448 kB
  • sloc: ansic: 75,559; cpp: 43,314; sh: 9,096; javascript: 7,710; python: 2,538; makefile: 1,461; objc: 709; perl: 367; xml: 18
file content (173 lines) | stat: -rw-r--r-- 4,352 bytes parent folder | download
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
# Test the `argparse` builtin.

##########
# Start by verifying a bunch of error conditions.

logmsg No args is an error
argparse

logmsg Missing -- is an error
argparse h/help

logmsg Flags but no option specs is an error
argparse -s -- hello

logmsg Invalid option specs
argparse h-
argparse +help
argparse h/help:
argparse h-help::
argparse h-help=x

logmsg --max-args and --min-args work
begin
    argparse --name min-max --min-args 1 h/help --
    argparse --name min-max --min-args 1 --max-args 3 h/help -- arg1
    argparse --name min-max --min-args 1 --max-args 3 h/help -- arg1 arg2
    argparse --name min-max --min-args 1 --max-args 3 h/help -- --help arg1 arg2 arg3
    argparse --name min-max --min-args 1 --max-args 3 h/help -- arg1 arg2 -h arg3 arg4
    argparse --name min-max --max-args 1 h/help --
    argparse --name min-max --max-args 1 h/help -- arg1
    argparse --name min-max --max-args 1 h/help -- arg1 arg2
end

logmsg Invalid \"#-val\" spec
begin
    argparse '#-val=' -- abc -x def
end

logmsg Invalid arg in the face of a \"#-val\" spec
begin
    argparse '#-val' -- abc -x def
end

logmsg Defining a short flag more than once
begin
    argparse 's/short' 'x/xray' 's/long' -- -s -x --long
end

logmsg Defining a long flag more than once
begin
    argparse 's/short' 'x/xray' 'l/short' -- -s -x --long
end

logmsg Defining an implicit int flag more than once
begin
    argparse '#-val' 'x/xray' 'v#val' -- -s -x --long
end

logmsg Defining an implicit int flag with modifiers
begin
    argparse 'v#val=' --
end

##########
# Now verify that validly formed invocations work as expected.

logmsg No args
begin
    argparse h/help --
end

logmsg One arg and no matching flags
begin
    argparse h/help -- help
    set -l
end

logmsg Five args with two matching a flag
begin
    argparse h/help -- help --help me -h 'a lot more'
    set -l
end

logmsg Required, optional, and multiple flags
begin
    argparse 'h/help' 'a/abc=' 'd/def=?' 'g/ghk=+' -- help --help me --ghk=g1 --abc=ABC --ghk g2 --d -g g3
    set -l
end

logmsg --stop-nonopt works
begin
    argparse --stop-nonopt 'h/help' 'a/abc=' -- -a A1 -h --abc A2 non-opt 'second non-opt' --help
    set -l
end

logmsg Implicit int flags work
begin
    argparse '#-val' -- abc -123 def
    set -l
end
begin
    argparse 'v/verbose' '#-val' 't/token=' -- -123 a1 --token woohoo --234 -v a2 --verbose
    set -l
end

logmsg Should be set to 987
begin
    argparse 'm#max' -- argle -987 bargle
    set -l
end

logmsg Should be set to 765
begin
    argparse 'm#max' -- argle -987 bargle --max 765
    set -l
end

logmsg Bool short flag only
begin
    argparse 'C' 'v' -- -C -v arg1 -v arg2
    set -l
end

logmsg Value taking short flag only
begin
    argparse 'x=' 'v/verbose' -- --verbose arg1 -v -x arg2
    set -l
end

logmsg Implicit int short flag only
begin
    argparse 'x#' 'v/verbose' -- -v -v argle -v -x 321 bargle
    set -l
end

logmsg Implicit int short flag only with custom validation passes
begin
    argparse 'x#!_validate_int --max 500' 'v/verbose' -- -v -v -x 499 -v
    set -l
end

logmsg Implicit int short flag only with custom validation fails
begin
    argparse 'x#!_validate_int --min 500' 'v/verbose' -- -v -v -x 499 -v
    set -l
end

##########
# Verify that flag value validation works.

logmsg Implicit int flag validation fails
argparse 'm#max' -- argle --max 765x bargle
and echo unxpected argparse return status >&2
argparse 'm#max' -- argle -ma1 bargle
and echo unxpected argparse return status >&2

logmsg Check the exit status from argparse validation
argparse 'm#max!set | grep _flag_; function x; return 57; end; x' -- argle --max=83 bargle 2>&1
set -l saved_status $status
test $saved_status -eq 57
and echo expected argparse return status $saved_status

logmsg Explicit int flag validation
# These should fail
argparse 'm#max!_validate_int --min 1 --max 1' -- argle -m2 bargle
and echo unexpected argparse return status $status >&2
argparse 'm#max!_validate_int --min 0 --max 1' -- argle --max=-1 bargle
and echo unexpected argparse return status $status >&2
# These should succeed
argparse 'm/max=!_validate_int --min 0 --max 1' -- argle --max=0 bargle
or echo unexpected argparse return status $status >&2
argparse 'm/max=!_validate_int --min 0 --max 1' -- argle --max=1 bargle
or echo unexpected argparse return status $status >&2