Skip to content

Instantly share code, notes, and snippets.

@carsonfarmer
Created December 22, 2018 00:36
Show Gist options
  • Save carsonfarmer/03f12e205e346b4beda10ed813edfa7b to your computer and use it in GitHub Desktop.
Save carsonfarmer/03f12e205e346b4beda10ed813edfa7b to your computer and use it in GitHub Desktop.
make commandline docs
import re
import subprocess
cmds = subprocess.check_output(["textile", "commands"]).decode("utf-8").split("\n")[:-1]
seen = set()
for cmd in cmds:
line = cmd.split(" ") + ["--help",]
if line:
if line[1] not in seen:
seen.add(line[1])
header = "# `{}`\n\n".format(line[1])
else:
header = ""
help = subprocess.check_output(line).decode("utf-8").split("\n")
# Make usage bold
help[0] = "**Usage:**\n"
# Backtick command name
help[1] = "`{}`".format(help[1][2:]).lower()
# Remove leading spaces
# print(help)
help = [re.sub(r'^[ \t]+', "", h) for h in help]
help = ["Command Options:" if h.endswith("command options]") else h+" " for h in help]
# Merge back into string
help = "\n".join(help)
help = help.replace("Command Options:\n\n", "")
# Regex caption options and add backticks
flag = re.compile(r"(-{1,2}\w*-?\w*=?)")
help = flag.sub(r"`\1`", help)
# Create subheade
if len(line) > 3:
header += "## `{}`\n\n".format(" ".join(line[1:-1]))
print(header + help)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment