Created
July 14, 2023 19:17
-
-
Save pcroland/5f27b7a88ce90e5ca2ee1db174ff6e35 to your computer and use it in GitHub Desktop.
Template for Python projects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import sys | |
from rich import print | |
from rich.containers import Lines | |
from rich.text import Text | |
from rich_argparse import RichHelpFormatter | |
prog_name = "sub-full-to-forced" | |
prog_version = "1.0.0" | |
class WrappedFormatter(RichHelpFormatter): | |
def _rich_split_lines(self, text: Text, width: int) -> Lines: | |
lines = Lines() | |
for line in text.split(): | |
lines.extend(line.wrap(self.console, width)) | |
return lines | |
def _rich_fill_text(self, text: Text, width: int, indent: Text) -> Text: | |
lines = self._rich_split_lines(text, width) | |
return Text("\n").join(indent + line for line in lines) + "\n" | |
RichHelpFormatter.styles["argparse.prog"] = "cyan" | |
RichHelpFormatter.styles["argparse.args"] = "green" | |
RichHelpFormatter.styles["argparse.metavar"] = "bold color(231)" | |
RichHelpFormatter.group_name_formatter = str.upper | |
parser = argparse.ArgumentParser( | |
prog=prog_name, | |
add_help=False, | |
formatter_class=lambda prog: WrappedFormatter(prog, max_help_position=23, width=min(120, os.get_terminal_size()[0])), | |
) | |
parser.add_argument("-h", "--help", | |
action="help", | |
default=argparse.SUPPRESS, | |
help="show this help message.") | |
parser.add_argument("-v", "--version", | |
action="version", | |
version=f"[cyan]{prog_name}[/cyan] [bold color(231)]{prog_version}[/bold color(231)]", | |
help="show version.") | |
parser.add_argument("-i", "--input", | |
type=str, | |
default="", | |
help="input path") | |
parser.add_argument("-o", "--output", | |
type=str, | |
default="", | |
help="output path") | |
args = parser.parse_args() | |
def main(): | |
if len(sys.argv) == 1: | |
parser.print_help(sys.stderr) | |
sys.exit(1) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment