Verified Commit 638b7c70 authored by Toby Hodges's avatar Toby Hodges Committed by Renato Alves
Browse files

add solution to 4.7

parent 3069bf5f
......@@ -35,15 +35,6 @@ In this section, we'll explore two options available in the standard library
for handling information provided by the user to our program
as part of the command line used to execute the script.
- [`sys.argv`](#simple-command-line-argument-access-with-sysargv)
- [`argparse`](#handling-options-and-arguments-with-argparse)
- [Positional arguments](#positional-arguments)
- [Options](#options)
- [Restricting input values](#restricting-input-values)
- [Default values](#default-values)
- [Capturing multiple values](#capturing-multiple-values)
- [Mutually exclusive arguments](#mutually-exclusive-arguments)
## Simple Command Line Argument Access with `sys.argv`
The `argv` object within the [`sys`][sys-module] module
......@@ -1034,7 +1025,7 @@ optional arguments:
> > import argparse
> >
> > parser = argparse.ArgumentParser(
> > description="Do something to data in (possible compressed) files, involving quality scores and sample names."
> > description="Do something to data in (possibly compressed) files, involving quality scores and sample names."
> > )
> > parser.add_argument("files", metavar="FILE", nargs="+",
> > help="input files for processing.")
......@@ -1090,9 +1081,9 @@ optional arguments:
>
> As a reminder, after the changes you made earlier,
> the program should take
> **one (and _only_ one) of three modes** (`--sum`, `--product`, `--mean`)
> one **(and now _only_ one)** of three modes (`--sum`, `--product`, `--mean`)
> to determine which function will be used in the body of the script,
> **an arbitrary number of input files**,
> **at least one input file**,
> and, **optionally, an output file name**.
> Also include the possibility to provide shorthand, single-letter flags
> (`-s`, `-p`, and `-m`)
......@@ -1106,6 +1097,35 @@ optional arguments:
> Do you think this is better than the approach we used before?
> If so, do you consider the improvement sufficient to be worth the effort?
>
> > ## Solution
> >
> > ~~~
> > import argparse
> >
> > parser = argparse.ArgumentParser()
> > parser.add_argument('--output_file', "-o", default='',
> > help="path to output file. (default: write to STDOUT)")
> > parser.add_argument("input_file", nargs='+',
> > help="path to input file(s)")
> >
> > modes = parser.add_mutually_exclusive_group()
> > modes.add_argument("--sum", "-s", action='store_true',
> > help="run in 'sum' mode.")
> > modes.add_argument("--product", "-p", action='store_true',
> > help="run in 'product' mode.")
> > modes.add_argument("--mean", "-m", action='store_true',
> > help="run in 'mean' mode.")
> > args = parser.parse_args()
> > ~~~
> > {: .language-python}
> >
> > Note that another approach,
> > which might have advantages when trying to implement the different modes
> > within the body of the program,
> > would be to replace the three options in the `modes` group with
> > a single, required, `mode` argument accepting three `choices`:
> > `sum`, `mean`, and `product`.
> {: .solution}
{: .challenge }
{% include links.md %}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment