Skip to content

Commit 4b41ae5

Browse files
committed
Fallback to argparse if configargparse not available
1 parent 736cc70 commit 4b41ae5

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

fprettify/__init__.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
import logging
7272
import os
7373
import io
74-
import configargparse
7574

7675
# allow for unicode for stdin / stdout, it's a mess
7776
try:
@@ -1422,6 +1421,11 @@ def log_message(message, level, filename, line_nr):
14221421
def run(argv=sys.argv): # pragma: no cover
14231422
"""Command line interface"""
14241423

1424+
try:
1425+
import configargparse as argparse
1426+
except ImportError:
1427+
import argparse
1428+
14251429
def str2bool(str):
14261430
"""helper function to convert strings to bool"""
14271431
if str.lower() in ('yes', 'true', 't', 'y', '1'):
@@ -1431,10 +1435,14 @@ def str2bool(str):
14311435
else:
14321436
return None
14331437

1434-
parser = configargparse.ArgumentParser(prog=argv[0],
1435-
description='Auto-format modern Fortran source files.',
1436-
default_config_files=['.fprettify.rc', '~/.fprettify.rc'],
1437-
formatter_class=configargparse.ArgumentDefaultsHelpFormatter)
1438+
arguments = {'prog': argv[0],
1439+
'description': 'Auto-format modern Fortran source files.',
1440+
'formatter_class': argparse.ArgumentDefaultsHelpFormatter}
1441+
1442+
if argparse.__name__ == "configargparse":
1443+
arguments['default_config_files'] = ['.fprettify.rc', '~/.fprettify.rc']
1444+
1445+
parser = argparse.ArgumentParser(**arguments)
14381446

14391447
parser.add_argument("-i", "--indent", type=int, default=3,
14401448
help="relative indentation width")
@@ -1478,7 +1486,7 @@ def str2bool(str):
14781486
group.add_argument("-S", "--silent", "--no-report-errors", action='store_true',
14791487
default=False, help="Don't write any errors or warnings to stderr")
14801488
group.add_argument("-D", "--debug", action='store_true',
1481-
default=False, help=configargparse.SUPPRESS)
1489+
default=False, help=argparse.SUPPRESS)
14821490
parser.add_argument("path", type=str, nargs='*',
14831491
help="Paths to files to be formatted inplace. If no paths are given, stdin (-) is used by default. Path can be a directory if --recursive is used.", default=['-'])
14841492
parser.add_argument('-r', '--recursive', action='store_true',

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
setuptools
2+
configargparse

0 commit comments

Comments
 (0)