Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Bio-IT Workshops
Intermediate Python
Commits
638b7c70
Verified
Commit
638b7c70
authored
Jul 08, 2020
by
Toby Hodges
Committed by
Renato Alves
Jul 20, 2020
Browse files
add solution to 4.7
parent
3069bf5f
Changes
1
Hide whitespace changes
Inline
Side-by-side
_episodes/04-argparse.md
View file @
638b7c70
...
...
@@ -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 (possibl
e
compressed) files, involving quality scores and sample names."
> > description="Do something to data in (possibl
y
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,
> **a
n arbitrary number of
input file
s
**,
> **a
t 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 %}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment