Commit 003cf587 authored by Theophile GENTILHOMME's avatar Theophile GENTILHOMME

Add dump config file

parent 13a70cfc
Pipeline #21600 failed with stage
in 14 minutes and 20 seconds
from ..log import set_verbosity_level
from ..config import load, mod_to_context
import time
import click
import logging
......@@ -172,6 +173,9 @@ class ConfigCommand(click.Command):
# Add the config argument to the command
click.argument(config_argument_name, nargs=-1)(self)
# Option for config file generation
click.option('-dc', '--dump-config', type=click.Path(exists=False),
help="Name of the config file to be generated")(self)
def invoke(self, ctx):
config_files = ctx.params[self.config_argument_name.lower()]
......@@ -180,7 +184,7 @@ class ConfigCommand(click.Command):
config_files, entry_point_group=self.entry_point_group)
config_context = mod_to_context(config_context)
for param in self.params:
if not in ctx.params:
if not in ctx.params or == 'dump_config':
value = ctx.params[]
if not hasattr(param, 'user_provided'):
......@@ -200,9 +204,27 @@ class ConfigCommand(click.Command):
# make sure to set this back to False for future invocations
param.required = False
if 'dump_config' in ctx.params:
return super(ConfigCommand, self).invoke(ctx)
def dump_config(self, ctx):
config_file = open(ctx.params.get('dump_config'), 'w')
config_file.write('# Configuration file automatically generated at %s '
'for %s.\n\n\n' % (time.strftime("%d/%m/%Y"),
for param in self.params:
if not in ctx.params or == 'dump_config':
if not isinstance(param, click.Option):
config_file.write('# %s.\n\n' %
config_file.write('# %s = %s \n\n' % (,
class ResourceOption(click.Option):
"""A click.Option that is aware if the user actually provided this option
......@@ -3,7 +3,7 @@
import pkg_resources
import click
from click_plugins import with_plugins
from .click_helper import AliasedGroup
from .click_helper import AliasedGroup, ConfigCommand
from ..log import setup
logger = setup('bob')
......@@ -171,3 +171,20 @@ def test_prefix_aliasing():
result = runner.invoke(cli, ['test_a'], catch_exceptions=False)
assert result.exit_code == 0, (result.exit_code, result.output)
assert 'AAA' in result.output, (result.exit_code, result.output)
def test_config_dump():
def cli():
@click.option('-t', '--test', required=True, default="/my/path/test.txt",
help="Path leading to test blablabla", cls=ResourceOption)
def test(config, test, **kwargs):
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(cli, ['test', '-dc', 'TEST_CONF'], catch_exceptions=False)
assert result.exit_code != 0, (result.exit_code, result.output)
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment