Skip to content

Commit 7989eb6

Browse files
authored
Merge pull request #2 from landoskape/dev
Documentation improvements for 0.1.3
2 parents 53b9aa8 + 50666db commit 7989eb6

File tree

7 files changed

+58
-51
lines changed

7 files changed

+58
-51
lines changed

conditional_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from inspect import signature
55
from argparse import ArgumentParser, Namespace
66

7-
__version__ = "0.1.2"
7+
__version__ = "0.1.3"
88

99

1010
class ConditionalArgumentParser(ArgumentParser):

docs/source/api.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
API Reference
2+
=============
3+
4+
ConditionalArgumentParser
5+
-------------------------
6+
7+
.. autoclass:: conditional_parser.ConditionalArgumentParser
8+
:show-inheritance:
9+
:special-members: __init__
10+
:members: add_conditional, parse_args
11+
12+
13+
The ConditionalArgumentParser extends Python's ArgumentParser to support conditional
14+
arguments. It inherits all standard ArgumentParser functionality while adding the ability
15+
to specify arguments that only appear when certain conditions are met.
16+
17+
Key Methods
18+
-----------
19+
20+
The main addition to ArgumentParser is the :meth:`add_conditional` method, which allows you to add a conditional argument to the parser. This method takes the same arguments as the standard :meth:`add_argument` method, with the addition of a `dest` and `cond` arguments which specify the destination in the namespace to check for the condition and the condition itself, respectively.
21+
22+
.. note::
23+
The ``dest`` argument is the name of the argument in the namespace that will be checked for the condition. The nomenclature might be a bit confusing; it's used to be consistent with how the ``ArgumentParser`` assigns arguments to the namespace.
24+
25+
What you are looking for is whatever attribute name you'd use to get the value of the argument from the output of ``parse_args()``. So, for example:
26+
27+
.. code-block:: python
28+
29+
from argparse import ArgumentParser
30+
parser = ArgumentParser()
31+
parser.add_argument("--use-regularization", default=False, action="store_true")
32+
parser.add_argument("--nohyphen", default=False, action="store_true")
33+
args = parser.parse_args(["--use-regularization"])
34+
# The "dest" of --use-regularization is "use_regularization"
35+
print(args.use_regularization) # True
36+
# The "dest" of --nohyphen is "nohyphen"
37+
print(args.nohyphen) # False

docs/source/api/index.rst

Lines changed: 0 additions & 29 deletions
This file was deleted.

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
project = "Conditional Parser"
1414
copyright = "2024, Andrew T. Landau"
1515
author = "Andrew T. Landau"
16-
release = "0.1.2"
16+
release = "0.1.3"
1717

1818
# -- General configuration ---------------------------------------------------
1919
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

docs/source/index.rst

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
Conditional Parser
2-
=================
2+
==================
33

4-
A flexible extension to Python's ArgumentParser for conditional arguments.
4+
A flexible extension to Python's ``ArgumentParser`` that enables dynamic, conditional command-line arguments - arguments that only appear based on the values of other arguments.
5+
6+
The argparse module is fantastic, but it lacks the ability to flexibly create conditional arguments. While the native ``ArgumentParser`` supports the ability to make a single subcommand, it does not have the ability to make multiple conditional arguments in parallel, in hierarchical structures, or with complex conditional logic. The ``ConditionalArgumentParser`` extends the native ``ArgumentParser`` to support these features while maintaining the familiar interface.
57

68
.. toctree::
7-
:maxdepth: 2
9+
:maxdepth: 1
810
:caption: Contents:
911

1012
installation
1113
quickstart
1214
examples/index
13-
api/index
14-
15-
About
16-
-----
17-
18-
The Conditional Parser package extends Python's native ArgumentParser to enable dynamic,
19-
conditional command-line arguments - arguments that only appear based on the values of
20-
other arguments.
15+
api
2116

2217
Key Features
23-
-----------
18+
------------
2419

2520
* Seamless extension of Python's ArgumentParser
2621
* Support for conditional arguments based on other argument values
@@ -29,7 +24,9 @@ Key Features
2924
* Compatible with existing ArgumentParser usage
3025

3126
Quick Example
32-
------------
27+
-------------
28+
29+
Suppose your parser includes a ``--use-regularization`` flag for training a machine learning model. When this flag is used, you'd probably want to include another argument ``--regularizer-lambda`` to control the regularization strength - but this parameter is only relevant when regularization is enabled. With ``ConditionalArgumentParser``, you can easily add this conditional argument.
3330

3431
.. code-block:: python
3532
@@ -38,10 +35,12 @@ Quick Example
3835
parser = ConditionalArgumentParser()
3936
parser.add_argument("--use-regularization", action="store_true")
4037
41-
# Will only add if --use-regularization is used, e.g. if use_regularization is set to True
38+
# Will only add if --use-regularization is used
39+
# E.g. if args.use_regularization == True
4240
parser.add_conditional(
43-
"use_regularization", True,
44-
"--regularizer-lambda",
41+
"use_regularization", True, # parent / condition
42+
"--regularizer-lambda", # conditional argument
43+
# parameters for the conditional argument - these are totally normal!
4544
type=float,
4645
default=0.01
4746
)

docs/source/installation.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Installation
2-
===========
2+
============
33

44
The package is available on PyPI and can be installed using pip:
55

@@ -8,12 +8,12 @@ The package is available on PyPI and can be installed using pip:
88
pip install conditional-parser
99
1010
Requirements
11-
-----------
11+
------------
1212

1313
Conditional Parser has no external dependencies beyond Python's standard library.
1414

1515
Development Installation
16-
----------------------
16+
------------------------
1717

1818
To install for development:
1919

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "conditional-parser"
7-
version = "0.1.2"
7+
version = "0.1.3"
88
authors = [
99
{name = "Andrew Landau", email = "andrew+tyler+landau+getridofthisanddtheplusses@gmail.com"},
1010
]

0 commit comments

Comments
 (0)