Skip to content

Commit ccfab9b

Browse files
committed
moved out from main project
1 parent e06800a commit ccfab9b

File tree

8 files changed

+297
-1
lines changed

8 files changed

+297
-1
lines changed

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"python.linting.flake8Enabled": true,
3+
"python.linting.pylintEnabled": false,
4+
"python.linting.enabled": true,
5+
"python.pythonPath": "/home/dazer/.local/share/virtualenvs/python-argparse-extras-1dpB_DZQ/bin/python"
6+
}

Pipfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[dev-packages]
7+
flake8 = "*"
8+
black = "*"
9+
setuptools = "*"
10+
wheel = "*"
11+
12+
[packages]
13+
14+
[requires]
15+
python_version = "3.6"
16+
17+
[pipenv]
18+
allow_prereleases = true

Pipfile.lock

Lines changed: 150 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,33 @@
11
# python-argparse-extras
2-
Some helpers for python's argparse
2+
3+
Some helpers for python's argparse.
4+
5+
Currently, this package provides 3 HelpFormatter classes that you can extend and / or use with
6+
python's `argparse` module:
7+
8+
## Contents
9+
10+
### `WideHelpFormatter`
11+
12+
HelpFormatter which tries to use a given amount of the terminal width.
13+
14+
The maximum line length is determines by the class attribute `percentage_of_line_length`.
15+
This defaults to the full width (1.0). Override this class and set your own percentage.
16+
17+
This uses ``shutil.get_terminal_size()`` to determine the line length.
18+
19+
### `ThreeQuarterWidthHelpFormatter`
20+
21+
A `WideHelpFormatter` subclass with its `percentage_of_line_length` set to three quarters of the terminal line length.
22+
23+
### `ThreeQuarterWidthDefaultsHelpFormatter`
24+
25+
A `ThreeQuarterWidthHelpFormatter` subclass which also derives from `argparse.ArgumentDefaultsHelpFormatter`. This means that in addition to using 3/4 of the terminal line length, it also prints the argument defaults in the help text.
26+
27+
## Usage
28+
29+
Use these classes in the same way as any other `argparse.HelpFormatter` subclass while creating your argument parser:
30+
31+
parser = argparse.ArgumentParser(formatter_class=<HelpFormatter>)
32+
parser.add_argument(...)
33+
...

apextras/__init__.py

Whitespace-only changes.

apextras/formatter/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import argparse
2+
from .wide import WideHelpFormatter
3+
4+
5+
class ThreeQuarterWidthHelpFormatter(WideHelpFormatter):
6+
percentage_of_line_length = 0.75
7+
8+
9+
class ThreeQuarterWidthDefaultsHelpFormatter(ThreeQuarterWidthHelpFormatter, argparse.ArgumentDefaultsHelpFormatter):
10+
pass
11+
12+
13+
__all__ = (
14+
WideHelpFormatter,
15+
ThreeQuarterWidthHelpFormatter,
16+
ThreeQuarterWidthDefaultsHelpFormatter
17+
)

apextras/formatter/wide.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import argparse
2+
import shutil
3+
4+
5+
class WideHelpFormatter(argparse.HelpFormatter):
6+
"""
7+
HelpFormatter which tries to use a given amount of the terminal width.
8+
9+
The maximum line length is determines by the class attribute ``percentage_of_line_length``.
10+
This defaults to the full width (1.0). Override this class and set your own percentage.
11+
12+
This uses ``shutil.get_terminal_size()`` to determine the line length.
13+
14+
Add more formatting features by adding more superclasses to your formatter:
15+
16+
>>> import argparse
17+
>>> class ThreeQuarterWidthDefaultsHelpFormatter(WideDefaulsHelpFormatter, argparse.ArgumentDefaultsHelpFormatter):
18+
... percentage_of_line_length = 0.75
19+
>>> parser = argparse.ArgumentParser(formatter_class=ThreeQuarterWidthDefaultsHelpFormatter)
20+
>>> arg = parser.add_argument(
21+
... "-l", "-long_help",
22+
... help="This is a really long help message but should be displayed in one line."
23+
... )
24+
>>> parser.parse_args(["-h"]) # doctest: +IGNORE_EXCEPTION_DETAIL
25+
Traceback (most recent call last):
26+
...
27+
SystemExit: 0
28+
usage: [-h] [-l L]
29+
30+
optional arguments:
31+
-h, --help show this help message and exit
32+
-l L, -long_help L This is a really long help message but should be displayed in one line. (default: None)
33+
34+
This is an example if the line length exceeds the maximum of 100%:
35+
>>> import argparse
36+
>>> class TooWideHelpFormatter(WideDefaulsHelpFormatter):
37+
... percentage_of_line_length = 1.5
38+
>>> parser = argparse.ArgumentParser(formatter_class=TooWideHelpFormatter) # doctest: +IGNORE_EXCEPTION_DETAIL
39+
Traceback (most recent call last):
40+
...
41+
ValueError: ValueError: Line length percentage can't exceed 1.0 (full width; actual value: 1.5)
42+
"""
43+
44+
percentage_of_line_length = 1.0
45+
46+
def __init__(self, *args, **kwargs):
47+
if self.percentage_of_line_length > 1.0:
48+
raise ValueError(
49+
f"Line length percentage can't exceed 1.0 (full width; actual value: {self.percentage_of_line_length})"
50+
)
51+
max_columns = int(shutil.get_terminal_size().columns * self.percentage_of_line_length)
52+
super().__init__(max_help_position=min(60, int(max_columns / 3)), width=max_columns, *args, **kwargs)

setup.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import setuptools
2+
3+
with open("README.md", "r") as fh:
4+
long_description = fh.read()
5+
6+
setuptools.setup(
7+
name="apextras",
8+
version="0.9.0",
9+
author="David Zerrenner",
10+
author_email="dazer017@gmail.com",
11+
description="Some helpers for python's argparse",
12+
long_description=long_description,
13+
long_description_content_type="text/markdown",
14+
url="https://github.com/pypyr-scheduler/python-argparse-extras",
15+
packages=setuptools.find_packages(),
16+
classifiers=[
17+
"Programming Language :: Python :: 3",
18+
"License :: OSI Approved :: MIT License",
19+
"Operating System :: OS Independent",
20+
],
21+
python_requires='>=3.6',
22+
)

0 commit comments

Comments
 (0)