From 7688567faa78946024d0c1de30bc72340c18fa3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Verg=C3=A9?= Date: Fri, 4 Mar 2016 18:49:29 +0100 Subject: [PATCH] cli: Add the `-d` option to provide inline conf --- README.rst | 3 +++ docs/configuration.rst | 26 +++++++++++++++++++++++++- yamllint/cli.py | 18 +++++++++++++++--- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 24816c2..3bcbd7e 100644 --- a/README.rst +++ b/README.rst @@ -57,6 +57,9 @@ Usage .. code:: bash + # Use a pre-defined lint configuration + yamllint -d relaxed file.yml + # Use a custom lint configuration yamllint -c ~/myconfig file.yml diff --git a/docs/configuration.rst b/docs/configuration.rst index 419806c..b9b0f74 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -8,7 +8,7 @@ settings can be gathered in a configuration file. To use a custom configuration file, either name it ``.yamllint`` in your working directory, or use the ``-c`` option: -:: +.. code:: bash yamllint -c ~/myconfig file.yml @@ -22,6 +22,15 @@ Unless told otherwise, yamllint uses its ``default`` configuration: Details on rules can be found on :doc:`the rules page `. +There is another pre-defined configuration named ``relaxed``. As its name +suggests, it is more tolerant. + +It can be chosen using: + +.. code:: bash + + yamllint -d relaxed file.yml + Extending the default configuration ----------------------------------- @@ -63,6 +72,21 @@ strict on block sequences indentation: indentation: indent-sequences: whatever +Custom configuration without a config file +------------------------------------------ + +It is possible -- although not recommended -- to pass custom configuration +options to yamllint with the ``-d`` (short for ``--config-data``) option. + +Its content can either be the name of a pre-defined conf (example: ``default`` +or ``relaxed``) or a serialized YAML object describing the configuration. + +For instance: + +.. code:: bash + + yamllint -d "{extends: relaxed, rules: {line-length: {max: 120}}}" file.yml + Errors and warnings ------------------- diff --git a/yamllint/cli.py b/yamllint/cli.py index 4e00d81..8c13678 100644 --- a/yamllint/cli.py +++ b/yamllint/cli.py @@ -66,8 +66,11 @@ def run(argv=None): description=APP_DESCRIPTION) parser.add_argument('files', metavar='FILE_OR_DIR', nargs='+', help='files to check') - parser.add_argument('-c', '--config', dest='config_file', action='store', - help='path to a custom configuration') + parser.add_argument('-c', '--config-file', dest='config_file', + action='store', help='path to a custom configuration') + parser.add_argument('-d', '--config-data', dest='config_data', + action='store', + help='custom configuration (as YAML source)') parser.add_argument('-f', '--format', choices=('parsable', 'standard'), default='standard', help='format for parsing output') @@ -78,8 +81,17 @@ def run(argv=None): args = parser.parse_args(argv) + if args.config_file is not None and args.config_data is not None: + print('Options --config-file and --config-data cannot be used ' + 'simultaneously.', file=sys.stderr) + sys.exit(-1) + try: - if args.config_file is not None: + if args.config_data is not None: + if ':' not in args.config_data: + args.config_data = 'extends: ' + args.config_data + conf = YamlLintConfig(content=args.config_data) + elif args.config_file is not None: conf = YamlLintConfig(file=args.config_file) elif os.path.isfile('.yamllint'): conf = YamlLintConfig(file='.yamllint')