Skip to content

Commit

Permalink
Added common options to CLI: -q & -v.
Browse files Browse the repository at this point in the history
The --quiet and --verbose options can be called from any command (parent or
subcommands), yet they are only defined once. Code adapted from:
pallets/click#108 (comment)

If either or both options are defined more than once by the user, the last
option defined is the only one which controls.

No support of -vvv to increase verbosity. MkDocks only utilizes a few
loging levels so the additional control offers no real value. Can always
be added later.
  • Loading branch information
waylan committed Jun 2, 2015
1 parent 596ab9f commit f79cc2a
Showing 1 changed file with 53 additions and 20 deletions.
73 changes: 53 additions & 20 deletions mkdocs/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,51 @@
log = logging.getLogger(__name__)


def configure_logging(log_name='mkdocs', level=logging.INFO):
'''When a --verbose flag is passed, increase the verbosity of mkdocs'''

logger = logging.getLogger(log_name)
logger.propagate = False
stream = logging.StreamHandler()
formatter = logging.Formatter("%(levelname)-7s - %(message)s ")
stream.setFormatter(formatter)
logger.addHandler(stream)

logger.setLevel(level)
class State(object):
''' Maintain logging level.'''

def __init__(self, log_name='mkdocs', level=logging.INFO):
self.logger = logging.getLogger(log_name)
self.logger.propagate = False
stream = logging.StreamHandler()
formatter = logging.Formatter("%(levelname)-7s - %(message)s ")
stream.setFormatter(formatter)
self.logger.addHandler(stream)

self.logger.setLevel(level)


pass_state = click.make_pass_decorator(State, ensure=True)


def verbose_option(f):
def callback(ctx, param, value):
state = ctx.ensure_object(State)
if value:
state.logger.setLevel(logging.DEBUG)
return click.option('-v', '--verbose',
is_flag=True,
expose_value=False,
help='Enable verbose output',
callback=callback)(f)


def quiet_option(f):
def callback(ctx, param, value):
state = ctx.ensure_object(State)
if value:
state.logger.setLevel(logging.ERROR)
return click.option('-q', '--quiet',
is_flag=True,
expose_value=False,
help='Silence warnings',
callback=callback)(f)


def common_options(f):
f = verbose_option(f)
f = quiet_option(f)
return f


clean_help = "Remove old files from the site_dir before building"
Expand All @@ -46,19 +80,13 @@ def configure_logging(log_name='mkdocs', level=logging.INFO):


@click.group(context_settings={'help_option_names': ['-h', '--help']})
@click.option('-v', '--verbose', is_flag=True, help="Enable verbose output")
@click.version_option(__version__, '-V', '--version')
def cli(verbose):
@common_options
def cli():
"""
MkDocs - Project documentation with Markdown.
"""

if verbose:
level = logging.DEBUG
else:
level = logging.INFO

configure_logging(level=level)
pass


@cli.command(name="serve")
Expand All @@ -67,6 +95,7 @@ def cli(verbose):
@click.option('-s', '--strict', is_flag=True, help=strict_help)
@click.option('-t', '--theme', type=click.Choice(theme_choices), help=theme_help)
@click.option('--livereload/--no-livereload', default=True, help=reload_help)
@common_options
def serve_command(dev_addr, config_file, strict, theme, livereload):
"""Run the builtin development server"""

Expand All @@ -91,6 +120,7 @@ def serve_command(dev_addr, config_file, strict, theme, livereload):
@click.option('-s', '--strict', is_flag=True, help=strict_help)
@click.option('-t', '--theme', type=click.Choice(theme_choices), help=theme_help)
@click.option('-d', '--site-dir', type=click.Path(), help=site_dir_help)
@common_options
def build_command(clean, config_file, strict, theme, site_dir):
"""Build the MkDocs documentation"""
try:
Expand All @@ -110,6 +140,7 @@ def build_command(clean, config_file, strict, theme, site_dir):
@click.option('-f', '--config-file', type=click.File('rb'), help=config_file_help)
@click.option('-s', '--strict', is_flag=True, help=strict_help)
@click.option('-d', '--site-dir', type=click.Path(), help=site_dir_help)
@common_options
def json_command(clean, config_file, strict, site_dir):
"""Build the MkDocs documentation to JSON files
Expand Down Expand Up @@ -139,6 +170,7 @@ def json_command(clean, config_file, strict, site_dir):
@click.option('-f', '--config-file', type=click.File('rb'), help=config_file_help)
@click.option('-m', '--message', help=commit_message_help)
@click.option('-b', '--remote-branch', help=remote_branch_help)
@common_options
def gh_deploy_command(config_file, clean, message, remote_branch):
"""Deply your documentation to GitHub Pages"""
try:
Expand All @@ -155,6 +187,7 @@ def gh_deploy_command(config_file, clean, message, remote_branch):

@cli.command(name="new")
@click.argument("project_directory")
@common_options
def new_command(project_directory):
"""Create a new MkDocs project"""
new.new(project_directory)

0 comments on commit f79cc2a

Please sign in to comment.