3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-23 19:19:31 +01:00

using-ircparser.md: minor tweaks and reordering

Thanks again to @IotaSpencer for writing this article :)
This commit is contained in:
James Lu 2017-03-12 20:45:42 -07:00 committed by GitHub
parent 75ea743b4a
commit 2e0c7db4e3

View File

@ -1,41 +1,38 @@
# Using utils.IRCParser()
**As of 22/02/2017 PyLink allows plugin creators to either parse command arguments themselves
**As of 22/02/2017 (1.2-dev), PyLink allows plugin creators to either parse command arguments themselves
or use a sub-classed instance of [argparse.ArgumentParser()](https://docs.python.org/3/library/argparse.html)
to parse their arguments.**
First off, you will already have access to IRCParser due to importing `utils`.
Otherwise, this is how to include it..
Otherwise, this is how to include it...
```python
from pylinkirc import utils
```
When you add a command that you want to use `utils.IRCParser()` on,
the following is a guide on how to add arguments.
When you add a command that you want to use `utils.IRCParser()` with, the following is a guide on how to add arguments.
**Note**: Most if not all the examples are from Python's argparse documentation, linked above.
#### Flag Arguments / 'Switch' Arguments
```python
SomeParser = utils.IRCParser()
SomeParser.addargument('-a', '--argumentname')
```
#### Named Arguments / Positional Arguments
#### Positional (Named) Arguments
```python
SomeParser.add_argument('argname')
```
#### Flag Arguments / Switch Arguments
```python
SomeParser = utils.IRCParser()
SomeParser.addargument('-a', '--argumentname')
```
##### Action
Actions define what to do when given an argument, whether it is used by itself or with some sort of value.
Actions define what to do when given an argument (i.e. whether it is used by itself or as some other sort of value).
`argparse` defines the following Actions.
Here are some of the actions that `argparse` defines:
* `store` - just stores the value given. This is the default when an action isn't provided.
```python
@ -57,7 +54,7 @@ Actions define what to do when given an argument, whether it is used by itself o
Namespace(foo=True, bar=False, baz=True)
```
* `append`
* `append` - additively stores arguments if a switch is given multiple times.
```python
>>> parser = argparse.ArgumentParser()
@ -66,7 +63,7 @@ Actions define what to do when given an argument, whether it is used by itself o
Namespace(foo=['1', '2'])
```
* `count` - counts how many times an argument was used (flag/switch arguments only)
* `count` - counts how many times an argument was used (for flag/switch arguments only)
```python
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--verbose', '-v', action='count')
@ -74,8 +71,6 @@ Actions define what to do when given an argument, whether it is used by itself o
Namespace(verbose=3)
```
Others exist, but are not particularly useful for the level of this guide.
You can also specify an arbitrary `Action` by sub-classing Action. If you want
to do this, you must `import argparse` in your plugin.
@ -116,13 +111,13 @@ SomeParser.add_argument('argname', choices=['A', 'AAAA', 'CNAME'])
The keyword argument `nargs` or Needed Args associates a different number of arguments to an action.
* `N` - this is an integer, N arguments will be gathered into a list. nargs=1 produces a list of one item, while the default (not using nargs) produces just the argument itself.
* `N` - this is an integer; N arguments will be gathered into a list. nargs=1 produces a list of one item, while the default (not using nargs) produces just the argument itself.
* `'?'` - One argument will be used, if `default` is defined in the call, then default will be used if there is no given argument.
* `'?'` - One argument will be used. If `default` is defined in the call, then default will be used if there is no given argument.
* `'*'` - All arguments are gathered into a list, while its *possible* to have multiple arguments with '*' it just doesn't make sense.
* `'*'` - All arguments are gathered into a list. It only makes sense to use this once in a command handler.
* `'+'` - Like '*' but raises an error if there wasn't at least one argument given.
* `utils.IRCParser.REMAINDER` - remaining arguments are gathered into a list, this is usually used when you need to get a phrase stored, such as the 'quote' text of a quote, a service bot part reason, and others.
* `utils.IRCParser.REMAINDER` - remaining arguments are gathered into a list; this is usually used when you need to get a phrase stored, such as the 'quote' text of a quote, a service bot part reason, etc. This is an alias to `argparse.REMAINDER`.