@@ -129,34 +129,73 @@ filename/filepath validator for argparse
129129
130130 from argparse import ArgumentParser
131131
132- from pathvalidate.argparse import filepath, filename
132+ from pathvalidate.argparse import validate_filename_arg, validate_filepath_arg
133133
134134 parser = ArgumentParser()
135- parser.add_argument(" --filepath" , type = filepath)
136- parser.add_argument(" --filename" , type = filename)
135+ parser.add_argument(" --filepath" , type = validate_filepath_arg)
136+ parser.add_argument(" --filename" , type = validate_filename_arg)
137+ options = parser.parse_args()
138+
139+ if options.filename:
140+ print (" filename: {} " .format(options.filename))
141+
142+ if options.filepath:
143+ print (" filepath: {} " .format(options.filepath))
137144
138145:Output:
139146 .. code-block ::
140147
148+ $ ./examples/argparse_validate.py --filename eg
149+ filename: eg
141150 $ ./examples/argparse_validate.py --filepath e?g
142151 usage: argparse_validate.py [-h] [--filepath FILEPATH] [--filename FILENAME]
143152 argparse_validate.py: error: argument --filepath: invalid char found: invalids=('?'), value='e?g', reason=INVALID_CHARACTER, target-platform=Windows
144153
154+ filename/filepath sanitizer for argparse
155+ ------------------------------------------
156+ :Sample Code:
157+ .. code-block :: python
158+
159+ from argparse import ArgumentParser
160+
161+ from pathvalidate.argparse import sanitize_filename_arg, sanitize_filepath_arg
162+
163+
164+ parser = ArgumentParser()
165+ parser.add_argument(" --filename" , type = sanitize_filename_arg)
166+ parser.add_argument(" --filepath" , type = sanitize_filepath_arg)
167+ options = parser.parse_args()
168+
169+ if options.filename:
170+ print (" filename: {} " .format(options.filename))
171+
172+ if options.filepath:
173+ print (" filepath: {} " .format(options.filepath))
174+
175+ :Output:
176+ .. code-block ::
177+
178+ $ ./examples/argparse_sanitize.py --filename e/g
179+ filename: eg
180+
145181 filename/filepath validator for click
146182---------------------------------------
147183:Sample Code:
148184 .. code-block :: python
149185
150186 import click
151187
152- from pathvalidate.click import filename, filepath
188+ from pathvalidate.click import validate_filename_arg, validate_filepath_arg
153189
154190
155191 @click.command ()
156- @click.option (" --filename" , callback = filename )
157- @click.option (" --filepath" , callback = filepath )
192+ @click.option (" --filename" , callback = validate_filename_arg )
193+ @click.option (" --filepath" , callback = validate_filepath_arg )
158194 def cli (filename , filepath ):
159- click.echo(filename, filepath)
195+ if filename:
196+ click.echo(" filename: {} " .format(filename))
197+ if filepath:
198+ click.echo(" filepath: {} " .format(filepath))
160199
161200
162201 if __name__ == " __main__" :
@@ -165,11 +204,42 @@ filename/filepath validator for click
165204:Output:
166205 .. code-block ::
167206
207+ $ ./examples/click_validate.py --filename ab
208+ filename: ab
168209 $ ./examples/click_validate.py --filepath e?g
169210 Usage: click_validate.py [OPTIONS]
170211
171212 Error: Invalid value for "--filepath": invalid char found: invalids=('?'), value='e?g', reason=INVALID_CHARACTER, target-platform=Windows
172213
214+ filename/filepath sanitizer for click
215+ ---------------------------------------
216+ :Sample Code:
217+ .. code-block :: python
218+
219+ import click
220+
221+ from pathvalidate.click import sanitize_filename_arg, sanitize_filepath_arg
222+
223+
224+ @click.command ()
225+ @click.option (" --filename" , callback = sanitize_filename_arg)
226+ @click.option (" --filepath" , callback = sanitize_filepath_arg)
227+ def cli (filename , filepath ):
228+ if filename:
229+ click.echo(" filename: {} " .format(filename))
230+ if filepath:
231+ click.echo(" filepath: {} " .format(filepath))
232+
233+
234+ if __name__ == " __main__" :
235+ cli()
236+
237+ :Output:
238+ .. code-block ::
239+
240+ $ ./examples/click_sanitize.py --filename a/b
241+ filename: ab
242+
173243 For more information
174244----------------------
175245More examples can be found at
0 commit comments