<spanclass="line"><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> cli.log.info(</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'Hello, </span><spanstyle="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%s</span><spanstyle="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">!'</span><spanstyle="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, cli.config.hello.name)</span></span></code></pre></div><p>First we import the <code>cli</code> object from <code>milc</code>. This is how we interact with the user and control the script's behavior. We use <code>@cli.argument()</code> to define a command line flag, <code>--name</code>. This also creates a configuration variable named <code>hello.name</code> (and the corresponding <code>user.name</code>) which the user can set so they don't have to specify the argument. The <code>cli.subcommand()</code> decorator designates this function as a subcommand. The name of the subcommand will be taken from the name of the function.</p><p>Once inside our function we find a typical "Hello, World!" program. We use <code>cli.log</code> to access the underlying <ahref="https://docs.python.org/3.7/library/logging.html#logger-objects"target="_blank"rel="noreferrer">Logger Object</a>, whose behavior is user controllable. We also access the value for name supplied by the user as <code>cli.config.hello.name</code>. The value for <code>cli.config.hello.name</code> will be determined by looking at the <code>--name</code> argument supplied by the user, if not provided it will use the value in the <code>qmk.ini</code> config file, and if neither of those is provided it will fall back to the default supplied in the <code>cli.argument()</code> decorator.</p><h1id="user-interaction"tabindex="-1">User Interaction <aclass="header-anchor"href="#user-interaction"aria-label="Permalink to "User Interaction""></a></h1><p>MILC and the QMK CLI have several nice tools for interacting with the user. Using these standard tools will allow you to colorize your text for easier interactions, and allow the user to control when and how that information is displayed and stored.</p><h2id="printing-text"tabindex="-1">Printing Text <aclass="header-anchor"href="#printing-text"aria-label="Permalink to "Printing Text""></a></h2><p>There are two main methods for outputting text in a subcommand- <code>cli.log</code> and <code>cli.echo()</code>. They operate in similar ways but you should prefer to use <code>cli.log.info()</code> for most general purpose printing.</p><p>You can use special tokens to colorize your text, to make it easier to understand the output of your program. See <ahref="#colorizing-text">Colorizing Text</a> below.</p><p>Both of these methods support built-in string formatting using python's <ahref="https://docs.python.org/3.7/library/stdtypes.html#old-string-formatting"target="_blank"rel="noreferrer">printf style string format operations</a>. You can use tokens such as <code>%s</code> and <code>%d</code> within your text strings then pass the values as arguments. See our Hello, World program above for an example.</p><p>You should never use the format operator (<code>%</code>) directly, always pass values as arguments.</p><h3id="logging-cli-log"tabindex="-1">Logging (<code>cli.log</code>) <aclass="header-anchor"href="#logging-cli-log"aria-label="Permalink to "Logging (`cli.log`)""></a></h3><p>The <code>cli.log</code> object gives you access to a <ahref="https://docs.python.org/3.7/library/logging.html#logger-objects"target="_blank"rel="noreferrer">Logger Object</a>. We have configured our log output to show the user a nice emoji for each log level (or the log level name if their terminal does not support unicode.) This way the user can tell at a glance which messages are most important when something goes wrong.</p><p>The default log level is <code>INFO</code>. If the user runs <code>qmk -v <subcommand></code> the default log level will be set to <code>DEBUG</code>.</p><table><thead><tr><th>Fun
<spanclass="line"><span> for key in cli.config[section]:</span></span>
<spanclass="line"><span> cli.log.info('%s.%s: %s', section, key, cli.config[section][key])</span></span></code></pre></div><h2id="setting-configuration-values"tabindex="-1">Setting Configuration Values <aclass="header-anchor"href="#setting-configuration-values"aria-label="Permalink to "Setting Configuration Values""></a></h2><p>You can set configuration values in the usual ways.</p><p>Dictionary style:</p><divclass="language- vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang"></span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><span>cli.config['<section>']['<key>'] = <value></span></span></code></pre></div><p>Attribute style:</p><divclass="language- vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang"></span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><span>cli.config.<section>.<key> = <value></span></span></code></pre></div><h2id="deleting-configuration-values"tabindex="-1">Deleting Configuration Values <aclass="header-anchor"href="#deleting-configuration-values"aria-label="Permalink to "Deleting Configuration Values""></a></h2><p>You can delete configuration values in the usual ways.</p><p>Dictionary style:</p><divclass="language- vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang"></span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><span>del(cli.config['<section>']['<key>'])</span></span></code></pre></div><p>Attribute style:</p><divclass="language- vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang"></span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><span>del(cli.config.<section>.<key>)</span></span></code></pre></div><h2id="writing-the-configuration-file"tabindex="-1">Writing The Configuration File <aclass="header-anchor"href="#writing-the-configuration-file"aria-label="Permalink to "Writing The Configuration File""></a></h2><p>The configuration is not written out when it is changed. Most commands do not need to do this. We prefer to have the user change their configuration deliberately using <code>qmk config</code>.</p><p>You can use <code>cli.save_config()</code> to write out the configuration.</p><h2id="excluding-arguments-from-configuration"tabindex="-1">Excluding Arguments From Configuration <aclass="header-anchor"href="#excluding-arguments-from-configuration"aria-label="Permalink to "Excluding Arguments From Configuration""></a></h2><p>Some arguments should not be propagated to the configuration file. These can be excluded by adding <code>arg_only=True</code> when creating the argument.</p><p>Example:</p><divclass="language- vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang"></span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><span>@cli.argument('-o', '--output', arg_only=True, help='File to write to')</span></span>
<spanclass="line"><span> pass</span></span></code></pre></div><p>You will only be able to access these arguments using <code>cli.args</code>. For example:</p><divclass="language- vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang"></span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><span>cli.log.info('Reading from %s and writing to %s', cli.args.filename, cli.args.output)</span></span></code></pre></div><h1id="testing-and-linting-and-formatting-oh-my"tabindex="-1">Testing, and Linting, and Formatting (oh my!) <aclass="header-anchor"href="#testing-and-linting-and-formatting-oh-my"aria-label="Permalink to "Testing, and Linting, and Formatting (oh my!)""></a></h1><p>We use nose2, flake8, and yapf to test, lint, and format code. You can use the <code>pytest</code> and <code>format-python</code> subcommands to run these tests:</p><h3id="testing-and-linting"tabindex="-1">Testing and Linting <aclass="header-anchor"href="#testing-and-linting"aria-label="Permalink to "Testing and Linting""></a></h3><divclass="language- vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang"></span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><span>qmk pytest</span></span></code></pre></div><h3id="formatting"tabindex="-1">Formatting <aclass="header-anchor"href="#formatting"aria-label="Permalink to "Formatting""></a></h3><divclass="language- vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang"></span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><span>qmk format-python</span></span></code></pre></div><h2id="formatting-details"tabindex="-1">Formatting Details <aclass="header-anchor"href="#formatting-details"aria-label="Permalink to "Formatting Details""></a></h2><p>We use <ahref="https://github.com/google/yapf"target="_blank"rel="noreferrer">yapf</a> to automatically format code. Our configuration is in the <code>[yapf]</code> section of <code>setup.cfg</code>.</p><divclass="tip custom-block"><pclass="custom-block-title">TIP</p><p>Many editors can use yapf as a plugin to automatically format code as you type.</p></div><h2id="testing-details"tabindex="-1">Testing Details <aclass="header-anchor"href="#testing-details"aria-label="Permalink to "Testing Details""></a></h2><p>Our tests can be found in <code>lib/python/qmk/tests/</code>. You will find both unit and integration tests in this directory. We hope you will write both unit and integration tests for your code, but if you do not please favor integration tests.</p><p>If your PR does not include a comprehensive set of tests please add comments like this to your code so that other people know where they can help:</p><divclass="language-python vp-adaptive-theme"><buttontitle="Copy Code"class="copy"></button><spanclass="lang">python</span><preclass="shiki shiki-themes github-light github-dark vp-code"><code><spanclass="line"><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># </span><spanstyle="--shiki-light:#D73A49;--shiki-dark:#F97583;">TODO</span><spanstyle="--shiki-light:#6A737D;--shiki-dark:#6A737D;">(unassigned/<your_github_username>): Write <unit|integration> tests</span></span></code></pre></div><p>We use <ahref="https://nose2.readthedocs.io/en/latest/getting_started.html"target="_blank"rel="noreferrer">nose2</a> to run our tests. You can refer to the nose2 documentation for more details on what you can do in your test functions.</p><h2id="linting-details"tabindex="-1">Linting Details <aclass="header-anchor"href="#linting-details"aria-label="Permalink to "Linting Details""></a></h2><p>We use flake8 to lint our code. Your code should pass flake8 before you open a PR. This will be checked when you run <code>qmk pytest</code> and by CI when you submit a PR.</p></div></div></main><footerclass="VPDocFooter"data-v-