Skip to content

Commit

Permalink
Improve filename handling a bit (FILE option argument)
Browse files Browse the repository at this point in the history
  • Loading branch information
eriknyquist committed Oct 11, 2023
1 parent 21e808c commit 1d255a1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
28 changes: 18 additions & 10 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ you want your program to accept, and ``duckargs`` will print the corresponding p

::

$ python -m duckargs positional_arg1 positional_arg2 -i --int-val 4 -f 3.3 -F --file file_that_exists -a -b -c > program.py
$ python -m duckargs positional_arg1 positional_arg2 -i --int-val 4 -f 3.3 -f --file FILE -F --otherfile FILE -a -b -c


After running the above command, the file ``program.py`` will contain the following code:
The output of the above command looks like this:


.. code:: python
# positional_arg1 positional_arg2 -i --int-val 4 -f 3.3 -F --file file_that_exists -a -b -c
# positional_arg1 positional_arg2 -i --int-val 4 -f 3.3 -f --file FILE -F --otherfile FILE -a -b -c
import argparse
Expand All @@ -51,7 +51,8 @@ After running the above command, the file ``program.py`` will contain the follow
parser.add_argument('positional_arg2', help='a string')
parser.add_argument('-i', '--int-val', default=4, type=int, help='an int value')
parser.add_argument('-f', default=3.3, type=float, help='a float value')
parser.add_argument('-F', '--file', default='file_that_exists', type=argparse.FileType(), help='a filename')
parser.add_argument('-f', '--file', default=None, type=argparse.FileType(), help='a filename')
parser.add_argument('-F', '--otherfile', default=None, type=argparse.FileType(), help='a filename')
parser.add_argument('-a', action='store_true', help='a flag')
parser.add_argument('-b', action='store_true', help='b flag')
parser.add_argument('-c', action='store_true', help='c flag')
Expand All @@ -62,6 +63,7 @@ After running the above command, the file ``program.py`` will contain the follow
print(args.int_val)
print(args.f)
print(args.file)
print(args.otherfile)
print(args.a)
print(args.b)
print(args.c)
Expand All @@ -80,16 +82,22 @@ will use the comma-separated values as a ``choices`` list for argparse, e.g.:

parser.add_argument('-m', '--mode', choices=['active', 'idle', 'sim'], default='active', help='a string')

Real filename for option argument
=================================
Filenames for option arguments
==============================

If you have an option which accepts an argument, and the argument string that you write
happens to be the path to a file that actually exists (e.g. ``-f --filename real_file.txt``),
then ``duckargs`` will tell argparse that this argument is a file, e.g.:
If you have an option that you want to accept a filename, you have two ways to tell
``duckargs`` that the option argument should be treated as a file:

* Pass the path to a file that actually exists (e.g. ``-f --filename file.txt``)
as the option argument

* Pass ``FILE`` as the option argument (e.g. ``-f --filename FILE``)

Either of which will generate a line like this:

::

parser.add_argument('-f', '--filename', default='real_file.txt', type=argparse.FileType(), help='a filename')
parser.add_argument('-f', '--filename', default='file', type=argparse.FileType(), help='a filename')


Use duckargs in python code
Expand Down
6 changes: 4 additions & 2 deletions duckargs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def finalize(self):
self.type = ArgType.FLOAT

if self.type is None:
if os.path.isfile(self.value):
if ('FILE' == self.value) or os.path.isfile(self.value):
self.type = ArgType.FILE
else:
self.type = ArgType.STRING
Expand Down Expand Up @@ -156,7 +156,9 @@ def generate_code(self):
else:
value = self.value

funcargs += f", default={value}"
default_str = "None" if self.type is ArgType.FILE else str(value)
funcargs += f", default={default_str}"

if self.type is not ArgType.STRING:
funcargs += f", type={self.type}"

Expand Down
2 changes: 1 addition & 1 deletion tests/test_data/readme_example/args.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
duckargs positional_arg1 positional_arg2 -i --int-val 4 -f 3.3 -F --file file_that_exists -a -b -c
duckargs positional_arg1 positional_arg2 -i --int-val 4 -f 3.3 -f --file FILE -F --otherfile FILE -a -b -c
6 changes: 4 additions & 2 deletions tests/test_data/readme_example/expected_python.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# positional_arg1 positional_arg2 -i --int-val 4 -f 3.3 -F --file file_that_exists -a -b -c
# positional_arg1 positional_arg2 -i --int-val 4 -f 3.3 -f --file FILE -F --otherfile FILE -a -b -c

import argparse

Expand All @@ -10,7 +10,8 @@ def main():
parser.add_argument('positional_arg2', help='a string')
parser.add_argument('-i', '--int-val', default=4, type=int, help='an int value')
parser.add_argument('-f', default=3.3, type=float, help='a float value')
parser.add_argument('-F', '--file', default='file_that_exists', type=argparse.FileType(), help='a filename')
parser.add_argument('-f', '--file', default=None, type=argparse.FileType(), help='a filename')
parser.add_argument('-F', '--otherfile', default=None, type=argparse.FileType(), help='a filename')
parser.add_argument('-a', action='store_true', help='a flag')
parser.add_argument('-b', action='store_true', help='b flag')
parser.add_argument('-c', action='store_true', help='c flag')
Expand All @@ -21,6 +22,7 @@ def main():
print(args.int_val)
print(args.f)
print(args.file)
print(args.otherfile)
print(args.a)
print(args.b)
print(args.c)
Expand Down

0 comments on commit 1d255a1

Please sign in to comment.