Skip to content

Instantly share code, notes, and snippets.

@CarterLi
Created November 17, 2023 08:35
Show Gist options
  • Save CarterLi/4d6c49686fa85f20a0b1b826e28ee548 to your computer and use it in GitHub Desktop.
Save CarterLi/4d6c49686fa85f20a0b1b826e28ee548 to your computer and use it in GitHub Desktop.
const str = ` -h, --help <?command>: Show this message, or help for a specific command
-v, --version: Show the version of fastfetch
--list-config-paths: List search paths of config files
--list-data-paths: List search paths of presets and logos
--list-logos: List available logos
--list-modules: List available modules
--list-presets: List presets fastfetch knows about; they can be loaded with --load-config (+)
--list-features: List the supported features fastfetch was compiled with (mainly for development)
--print-logos: Print available logos
--print-config-system: Print the default system config
--print-config-user: Print the default user config
--print-structure: Print the default structure`;
const flags = str
.trim()
.split("\n")
.map((x) => {
x = x.trim();
let i = x.indexOf(":");
return { left: x.slice(0, i), desc: x.slice(i + 1).trim() };
})
.map(({ left, desc }) => {
const types = /<(\??)(\w+)>$/.exec(left);
if (!types) return { flagText: left, desc, arg: null };
return { flagText: left.slice(0, types.index).trim(), desc, arg: { type: types[2], optional: !!types[1], default: types[2] === 'bool' ? false : '' } };
})
.map(({ flagText, desc, arg }) => {
const flags = flagText.split(',').map(x => x.trim());
if (flags.length === 1) {
return { short: null, long: flags[0].slice(2), desc, arg };
} else {
return { short: flags[0].slice(1), long: flags[1].slice(2), desc, arg }
}
});
console.log(JSON.stringify(flags, null, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment