-
Notifications
You must be signed in to change notification settings - Fork 494
Read options from config #1668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Read options from config #1668
Changes from all commits
04f8294
511958b
e35c22b
7b58596
96b8288
85812ec
3e40edd
a909625
efac968
98d96d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
|
||
import argparse | ||
import codecs | ||
import configparser | ||
import fnmatch | ||
import os | ||
import re | ||
|
@@ -361,12 +362,39 @@ def parse_options(args): | |
help='print LINES of leading context') | ||
parser.add_argument('-C', '--context', type=int, metavar='LINES', | ||
help='print LINES of surrounding context') | ||
parser.add_argument('--config', type=str, | ||
help='path to config file.') | ||
|
||
parser.add_argument('files', nargs='*', | ||
help='files or directories to check') | ||
|
||
# Parse command line options. | ||
options = parser.parse_args(list(args)) | ||
|
||
# Load config files and look for ``codespell`` options. | ||
cfg_files = ['setup.cfg', '.codespellrc'] | ||
if options.config: | ||
cfg_files.append(options.config) | ||
config = configparser.ConfigParser() | ||
config.read(cfg_files) | ||
|
||
if config.has_section('codespell'): | ||
# Build a "fake" argv list using option name and value. | ||
cfg_args = [] | ||
for key in config['codespell']: | ||
# Add option as arg. | ||
cfg_args.append("--%s" % key) | ||
# If value is blank, skip. | ||
val = config['codespell'][key] | ||
if val != "": | ||
cfg_args.append(val) | ||
|
||
# Parse config file options. | ||
options = parser.parse_args(cfg_args) | ||
|
||
# Re-parse command line options to override config. | ||
options = parser.parse_args(list(args), namespace=options) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than re-calling this, would it be better to do:
If it's equivalent, it seems simpler/cleaner There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It only re-parses if It's your project though, feel free to re-implement it yourself if you have a better idea. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh right, forgot about the need to parse to see the |
||
|
||
if not options.files: | ||
options.files.append('.') | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.