Skip to content

Commit

Permalink
minor leftover fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lubojr committed Jan 9, 2024
1 parent 27d8894 commit f7b1cf7
Showing 1 changed file with 15 additions and 39 deletions.
54 changes: 15 additions & 39 deletions lessons/beginners-en/argparse/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ you give to it as **arguments**.

### Example

An example of a tool which is usually installed on both Windows and Unix is ``curl``. It allows you to send HTTP requests to the internet and receive responses.

If you want to know what arguments does existing program allow, the argument to use is **help**. This works for most of programs on your computer (if their author created a help page). Also usually there is a `-help` or `--help` command line argument which also shows the expected usage of a tool to any user.

#### git
Expand All @@ -46,47 +44,25 @@ For example you can heavily customize output of `git log` in some existing git r
git log --oneline --graph --decorate --cherry-mark --boundary
```

Another example of a command line tool with arguments is **find** but does different things on Windows and Linux. This example is valid for Linux:

```console
find . -type f -name "*.txt" -empty
```

Which searches current directory for empty files with .txt suffix.

### Reading command line arguments

In order to read the command line arguments, we can use the `sys.argv` variable, which gives us a list of strings, one for each passed argument:

```python
# arguments.py
import sys

for arg in sys.argv:
print(arg)
```

When we execute this program with the passed in arguments, we can see them printed out:

```console
python3 arguments.py some test arguments "multi word argument"
some
test
arguments
multi word argument
```

However, this is not very convenient when we want to build actual CLI programs with options and arguments. But there are good tools we can use instead!
> [note]
> **Note about "raw" command line argument handling**
>
> In order to read the command line arguments, we can use the `sys.argv` variable, which gives us a list of strings, one for each passed argument:
> ```python
> # arguments.py
> import sys
>
> for arg in sys.argv:
> print(arg)
> ```
> When we execute this program with the passed in arguments, we can see them printed out.
> However, this is not very convenient when we want to build actual CLI programs with options and arguments. But there are good tools we can use instead!
### Argparse
How can we create a CLI in Python? Today, we will show usage of a `argparse` tool.

It is a part of the standard library, so you do not need to install anything extra.

You can find the official documentation on:
How can we create a CLI in Python? Today, we will show usage of a `argparse` tool. It is a part of the standard library, so you do not need to install anything extra.
- [argparse](https://docs.python.org/3/library/argparse.html)
You can find the official documentation here: [argparse](https://docs.python.org/3/library/argparse.html)
And a quite handy tutorial going through most of functionalities, you could ever encounter:
[argparse-tutorial](https://docs.python.org/3/howto/argparse.html)
Expand Down

0 comments on commit f7b1cf7

Please sign in to comment.