├── .browserslistrc ├── .eslintrc.js ├── .gitignore ├── LICENSE ├── README.md ├── assets └── favicon.png ├── package.json ├── public ├── favicon.png └── index.html ├── src ├── .babelrc ├── components │ ├── Footer.tsx │ ├── Navigation.tsx │ ├── Option.tsx │ ├── Options.tsx │ ├── Result.tsx │ └── index.ts ├── index.html ├── index.scss ├── index.tsx ├── model │ ├── dialects.ts │ ├── index.ts │ ├── options.ts │ └── result.ts ├── templates.d.ts ├── templates │ ├── gnu.hbs │ └── posix.hbs └── views │ ├── About.tsx │ ├── Generator.tsx │ └── index.tsx ├── tsconfig.json └── yarn.lock /.browserslistrc: -------------------------------------------------------------------------------- 1 | > 1% 2 | last 3 and_chr versions 3 | last 3 chrome versions 4 | last 3 opera versions 5 | last 3 ios_saf versions 6 | last 3 safari versions 7 | not ie <= 8 8 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: "@typescript-eslint/parser", 3 | parserOptions: { 4 | ecmaVersion: 2020, 5 | sourceType: "module" 6 | }, 7 | extends: [ 8 | "plugin:react/recommended", 9 | "plugin:@typescript-eslint/recommended", 10 | "plugin:prettier/recommended" 11 | ], 12 | settings: { 13 | react: { 14 | version: "detect", 15 | }, 16 | }, 17 | rules: { 18 | }, 19 | }; 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /dist 4 | 5 | # local env files 6 | .env.local 7 | .env.*.local 8 | 9 | # Log files 10 | npm-debug.log* 11 | yarn-debug.log* 12 | yarn-error.log* 13 | 14 | # Editor directories and files 15 | .idea 16 | .vscode 17 | *.suo 18 | *.ntvs* 19 | *.njsproj 20 | *.sln 21 | *.sw* 22 | 23 | .vercel.cache 24 | build 25 | .cache/ 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Wolfgang Werner 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![setup automated](https://img.shields.io/badge/Gitpod-ready_to_code-blue?logo=gitpod)](https://gitpod.io/from-referrer/) 2 | 3 | # Bashplate 4 | 5 | Bashplate generates bash boilerplate code for option parsing and validation. 6 | 7 | Currently deployed here: https://bashplate.wolfgang-werner.net 8 | 9 | Why? Each time (long enough to forget all the details after writing the last one) I need to write a shell script: 10 | * 🧐 "I want this to be reuseable, so I'll use proper options & help messages" 11 | * 🤔 "Hmmm, was it getopt or getopts? Which was the GNU and which the POSIX one? Shouldn't I use argbash?" 12 | * 😤 "So how does the option spec need to look like? Why is that colon at the start again? Does the following option take an argument?" and so on. 13 | * 😩 "How to I test if an argument was set? Reliably and portably?" 14 | 15 | Now, I either: 16 | * Copy an old script and fiddle around in it - bad, since there is stuff to remove, I could break things and I still need to think 17 | * Read up on option parsing - bad, as it takes time and I have to decide between getopt, getopts and argbash 18 | * Just skip parameterizing the script and hard code everything - bad, obviously 19 | 20 | So basically, I try to hack myself into writing better scripts. 21 | 22 | If you can relate, give it a spin. 23 | 24 | ## Requirements 25 | 26 | * POSIX compliance flavor, as I want to run in stripped down Docker containers 27 | * GNU flavor with long opts support, as I like short opts for one offs when typing, long opts in scripts for readability. 28 | * Minimum / No dependencies 29 | * Easy to put on the web, so I cannot lose it & don't have to install anything. 30 | 31 | ## Development 32 | 33 | Contributions welcome. 34 | 35 | * `yarn install` - Project setup 36 | * `yarn dev` - Compiles and hot-reloads for development 37 | * `yarn build` - Compiles and minifies for production 38 | * `yarn lint` - Lints code style issues (& fixes them where possible) 39 | 40 | Pushing to master deploys to https://bashplate.wolfgang-werner.net and https://bashplate.now.sh (until vercel decides to turn off now.sh). 41 | -------------------------------------------------------------------------------- /assets/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wwerner/bashplate/f2e7025e9f481de8a84cde607e4eadce489237f2/assets/favicon.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bashplate", 3 | "version": "2.0.0", 4 | "description": "Bashplate generates bash script boilerplate code for option parsing and validation", 5 | "main": "index.js", 6 | "repository": "git@github.com:wwerner/bashplate.git", 7 | "author": "Wolfgang Werner ", 8 | "license": "MIT", 9 | "private": false, 10 | "scripts": { 11 | "dev": "parcel src/index.html --out-dir build/debug --no-source-maps", 12 | "build": "parcel build src/index.html --out-dir dist --no-source-maps --public-url ./", 13 | "lint": "eslint 'src/**/*.{js,ts,tsx}' --fix" 14 | }, 15 | "dependencies": { 16 | "bulma": "^0.9.4", 17 | "components": "^0.1.0", 18 | "file-saver": "^2.0.5", 19 | "pizzly-js": "^0.2.8", 20 | "prismjs": "^1.28.0", 21 | "react": "^17.0.2", 22 | "react-dom": "^17.0.2", 23 | "react-router-dom": "^5.3.1" 24 | }, 25 | "devDependencies": { 26 | "@babel/core": "^7.12.16", 27 | "@types/file-saver": "^2.0.1", 28 | "@types/handlebars": "^4.1.0", 29 | "@types/prismjs": "^1.16.2", 30 | "@types/react": "^17.0.1", 31 | "@types/react-dom": "^17.0.0", 32 | "@types/react-router-dom": "^5.1.7", 33 | "@typescript-eslint/eslint-plugin": "^4.15.2", 34 | "@typescript-eslint/parser": "^4.15.2", 35 | "babel-plugin-prismjs": "^2.0.1", 36 | "eslint": "^7.20.0", 37 | "eslint-config-prettier": "^8.0.0", 38 | "eslint-plugin-prettier": "^3.3.1", 39 | "eslint-plugin-react": "^7.22.0", 40 | "parcel-bundler": "^1.12.4", 41 | "parcel-plugin-handlebars-precompile": "^1.0.2", 42 | "prettier": "^2.2.1", 43 | "sass": "^1.32.7", 44 | "typescript": "^4.1.5" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /public/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wwerner/bashplate/f2e7025e9f481de8a84cde607e4eadce489237f2/public/favicon.png -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 13 | 14 | 15 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | Bash Plate 33 | 34 | 35 | 38 |
39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /src/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | ["prismjs", { 4 | "languages": ["shell"], 5 | "plugins": ["line-numbers"], 6 | "theme": "tomorrow", 7 | "css": true 8 | }] 9 | ] 10 | } -------------------------------------------------------------------------------- /src/components/Footer.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react"; 2 | 3 | export const Footer = () => ( 4 | 23 | ); 24 | -------------------------------------------------------------------------------- /src/components/Navigation.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react"; 2 | import { NavLink } from "react-router-dom"; 3 | 4 | export const Navigation = () => ( 5 | 36 | ); 37 | -------------------------------------------------------------------------------- /src/components/Option.tsx: -------------------------------------------------------------------------------- 1 | import React, { ChangeEvent } from "react"; 2 | import { OptionData } from "~model"; 3 | 4 | interface OptionProps { 5 | data: OptionData; 6 | onChange: (data: OptionData) => void; 7 | onRemove: (data: OptionData) => void; 8 | } 9 | 10 | export const Option = ({ data: option, onChange, onRemove }: OptionProps) => { 11 | const updatedOptions = ( 12 | src: OptionData, 13 | updates: { [key: string]: boolean | string | number } 14 | ) => ({ ...src, ...updates }); 15 | 16 | const handleChange = (e: ChangeEvent) => { 17 | const value = 18 | e.currentTarget.type === "checkbox" 19 | ? e.currentTarget.checked 20 | : e.currentTarget.value; 21 | const field = e.currentTarget.name; 22 | 23 | switch (field) { 24 | case "isFlag": 25 | onChange( 26 | updatedOptions(option, { 27 | [field]: value, 28 | required: false, 29 | defaultValue: value ? "" : (option.defaultValue as string), 30 | }) 31 | ); 32 | break; 33 | default: 34 | onChange(updatedOptions(option, { [field]: value })); 35 | } 36 | }; 37 | 38 | const handleRemove = () => onRemove(option); 39 | 40 | return ( 41 | 42 | 43 | 50 | 51 | 52 | 61 | 62 | 63 | 72 | 73 | 74 | 83 | 84 | 85 | 91 | 92 | 93 | 103 | 104 | 105 | 114 | 115 | 116 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | ); 136 | }; 137 | -------------------------------------------------------------------------------- /src/components/Options.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react"; 2 | import { OptionData } from "~model"; 3 | import { Option } from "~/components"; 4 | 5 | interface OptionsProps { 6 | options: OptionData[]; 7 | onAddOption: () => void; 8 | onChangeOption: (data: OptionData) => void; 9 | onRemoveOption: (data: OptionData) => void; 10 | } 11 | 12 | export const Options = ({ 13 | options, 14 | onAddOption, 15 | onChangeOption, 16 | onRemoveOption, 17 | }: OptionsProps) => ( 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | {options.map((option) => ( 35 | 43 | 51 | 52 | 53 |
Req.ShortLongDescriptionFlagDefaultCallExit
44 | onAddOption()}> 45 | Add parameter 46 | 47 | 48 | 49 | 50 |
54 |
55 | ); 56 | -------------------------------------------------------------------------------- /src/components/Result.tsx: -------------------------------------------------------------------------------- 1 | import React, { useEffect } from "react"; 2 | import Prism from "prismjs"; 3 | import { ResultData, Dialects } from "~model"; 4 | 5 | interface ResultProps { 6 | result: ResultData; 7 | onChangeDialect: any; 8 | onCopy: () => void; 9 | onSave: () => void; 10 | onCreateGist: () => void; 11 | } 12 | 13 | export const Result = ({ 14 | result, 15 | onChangeDialect, 16 | onCopy, 17 | onSave, 18 | onCreateGist, 19 | }: ResultProps) => { 20 | useEffect(() => { 21 | setTimeout(() => Prism.highlightAll(), 0); 22 | }, [result.script]); 23 | 24 | return ( 25 |
26 |
27 | 45 |
46 | 47 |
48 |         
52 |           Copy 
53 |           
54 |             
55 |           
56 |         
57 |         
61 |           Download 
62 |           
63 |             
64 |           
65 |         
66 |         
70 |           Create Gist 
71 |           
72 |             
73 |           
74 |         
75 | 
76 |         {result.script}
77 |       
78 |
79 | ); 80 | }; 81 | -------------------------------------------------------------------------------- /src/components/index.ts: -------------------------------------------------------------------------------- 1 | export * from "./Navigation"; 2 | export * from "./Footer"; 3 | export * from "./Option"; 4 | export * from "./Options"; 5 | export * from "./Result"; 6 | -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 13 | 14 | 15 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | Bash Plate 32 | 33 | 34 | 37 |
38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/index.scss: -------------------------------------------------------------------------------- 1 | a.is-active { 2 | border-bottom: 1px solid black; 3 | } 4 | 5 | .footer { 6 | background-color: transparent; 7 | padding: 0.3rem 0.3rem 0.3rem 0.3rem; 8 | } 9 | 10 | /* override bulma's number style as it messes w/ prism's */ 11 | .number { 12 | -webkit-box-align: center; 13 | -ms-flex-align: center; 14 | align-items: center; 15 | background-color: transparent; 16 | border-radius: 0px; 17 | display: inline; 18 | font-size: 1rem; 19 | height: 1em; 20 | margin-right: 0rem; 21 | min-width: 0rem; 22 | padding: 0rem 0rem; 23 | vertical-align: middle; 24 | } 25 | 26 | .source-action-button { 27 | margin-left: 1rem; 28 | position: sticky; 29 | top: .05rem 30 | } 31 | 32 | #options { 33 | overflow: scroll 34 | } 35 | ::-webkit-scrollbar { 36 | display:none; 37 | } 38 | -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from "react"; 2 | import ReactDOM from "react-dom"; 3 | import FileSaver from "file-saver"; 4 | import Pizzly from "pizzly-js"; 5 | import "bulma"; 6 | import "./index.scss"; 7 | import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; 8 | import { Navigation, Footer } from "~components"; 9 | import { About, Generator } from "~views"; 10 | 11 | import { 12 | defaultScriptDescription, 13 | defaultOptions, 14 | OptionData, 15 | Dialects, 16 | templates, 17 | } from "~model"; 18 | 19 | const App = () => { 20 | const [options, setOptions] = useState(defaultOptions); 21 | const [dialect, setDialect] = useState(Dialects.posix); 22 | const [scriptDescription, setScriptDescription] = useState( 23 | defaultScriptDescription 24 | ); 25 | const [gist, setGist] = useState({}); 26 | const [gistSuccessModalVisible, setGistSuccessModalVisible] = useState(false); 27 | const [gistErrorModalVisible, setGistErrorModalVisible] = useState(false); 28 | 29 | const renderScript = (dialect: Dialects): string => 30 | templates[dialect]({ 31 | description: scriptDescription, 32 | requiredOptions: options.filter((o) => o.required), 33 | flagOptions: options.filter((o) => o.isFlag), 34 | parameterOptions: options.filter((o) => !o.isFlag), 35 | callingOptions: options.filter( 36 | (o) => 37 | o.functionCall && 38 | o.functionCall !== "usage" && 39 | o.functionCall !== "version" 40 | ), 41 | options: options.map((o) => ({ 42 | ...o, 43 | // trick handlebars into rendering 0 as exit code. 0 is false for #if, "0" isn't 44 | exitCode: o.exitCode === 0 ? "0" : o.exitCode, 45 | })), 46 | }); 47 | 48 | const [result, setResult] = useState({ 49 | description: scriptDescription, 50 | script: renderScript(dialect), 51 | dialect: dialect, 52 | }); 53 | 54 | useEffect(() => { 55 | setResult({ 56 | description: scriptDescription, 57 | script: renderScript(dialect), 58 | dialect: dialect, 59 | }); 60 | }, [options, dialect, scriptDescription]); 61 | 62 | const onRemoveOption = (option: OptionData) => 63 | setOptions(options.filter((o) => o.id !== option.id)); 64 | 65 | const onAddOption = () => 66 | setOptions( 67 | options.concat({ id: options[options.length - 1].id + 1 } as OptionData) 68 | ); 69 | 70 | const onChangeOption = (data: OptionData) => 71 | setOptions(options.map((o) => (o.id === data.id ? data : o))); 72 | 73 | const onChangeDialect = (dialect: Dialects) => setDialect(dialect); 74 | 75 | const onSave = () => { 76 | const blob = new Blob([result.script], { 77 | type: "text/plain;charset=utf-8", 78 | }); 79 | FileSaver.saveAs(blob, "bashplate.sh"); 80 | }; 81 | 82 | const onCopy = () => { 83 | navigator.clipboard.writeText(result.script); 84 | }; 85 | 86 | const onCreateGist = () => { 87 | const pizzly = new Pizzly({ 88 | host: "https://bashplate-oauth.herokuapp.com/", 89 | publishableKey: "poUDkGprMhTt87VZbttW", 90 | }); 91 | 92 | const github = pizzly.integration("github", { 93 | setupId: "329a658d-d273-4ce0-aa0e-dadf444930bf", 94 | }); 95 | 96 | github 97 | .connect() 98 | .then(({ authId }) => { 99 | const filename = `bashplate-${Date.now()}.sh`; 100 | console.log(authId, filename); 101 | 102 | const body = { 103 | description: result.description, 104 | public: true, 105 | files: { 106 | [filename]: { 107 | content: result.script, 108 | }, 109 | }, 110 | }; 111 | 112 | return github.auth(authId).post("/gists", { 113 | body: JSON.stringify(body), 114 | }); 115 | }) 116 | .then(async (res) => { 117 | const body = await res.json(); 118 | if (!res.ok) { 119 | throw new Error(body.message); 120 | } 121 | return body; 122 | }) 123 | .then((json) => { 124 | setGist(json); 125 | setGistSuccessModalVisible(true); 126 | }) 127 | .catch((err) => { 128 | setGist(err); 129 | setGistErrorModalVisible(true); 130 | }); 131 | }; 132 | 133 | const handleCloseGistDialog = () => { 134 | setGistErrorModalVisible(false); 135 | setGistSuccessModalVisible(false); 136 | setGist({}); 137 | }; 138 | 139 | return ( 140 |
141 | 142 | 143 | {/* TODO: Success Modal should go into new FeedbackModal component */} 144 |
145 |
149 |
150 |
151 |
152 |
153 | 154 |
155 |
156 |
157 |

158 | Yay! Your script has been published{" "} 159 | 167 | in your Gists 168 | 169 | . 170 |

171 |
172 |
173 |
174 |
175 |
176 | 181 |
182 | 183 | {/* TODO: Error Modal should go into new FeedbackModal component */} 184 |
185 |
189 |
190 |
191 |
192 |
193 | 194 |
195 |
196 |
197 |

Meh. That did not work.

198 |
199 |                       {
200 |                         // @ts-expect-error: Don't want to include GH API types now
201 |                         gist.message
202 |                       }
203 |                     
204 |
205 |
206 |
207 |
208 |
209 | 214 |
215 | 216 | 217 | 218 | 219 | 220 | 221 | 234 | 235 | 236 |
237 |
238 |
239 | ); 240 | }; 241 | 242 | ReactDOM.render(, document.getElementById("root")); 243 | -------------------------------------------------------------------------------- /src/model/dialects.ts: -------------------------------------------------------------------------------- 1 | import gnuTemplate from "~templates/gnu.hbs"; 2 | import posixTemplate from "~templates/posix.hbs"; 3 | 4 | export enum Dialects { 5 | gnu, 6 | posix, 7 | } 8 | 9 | export const templates: any = { 10 | [Dialects.gnu]: gnuTemplate, 11 | [Dialects.posix]: posixTemplate, 12 | }; 13 | -------------------------------------------------------------------------------- /src/model/index.ts: -------------------------------------------------------------------------------- 1 | export * from "./options"; 2 | export * from "./result"; 3 | export * from "./dialects"; 4 | -------------------------------------------------------------------------------- /src/model/options.ts: -------------------------------------------------------------------------------- 1 | export interface OptionData { 2 | id: number; 3 | required: boolean; 4 | shortName: string; 5 | longName: string; 6 | description: string; 7 | isFlag: boolean; 8 | defaultValue?: string; 9 | functionCall?: string; 10 | exitCode?: number; 11 | } 12 | 13 | export const defaultScriptDescription = "Script Description"; 14 | 15 | export const defaultOptions: OptionData[] = [ 16 | { 17 | id: 0, 18 | required: false, 19 | shortName: "h", 20 | longName: "help", 21 | description: "Show this help message.", 22 | isFlag: true, 23 | functionCall: "usage", 24 | exitCode: 0, 25 | }, 26 | { 27 | id: 1, 28 | required: false, 29 | shortName: "v", 30 | longName: "version", 31 | description: "Show version information.", 32 | isFlag: true, 33 | functionCall: "version", 34 | exitCode: 0, 35 | }, 36 | { 37 | id: 2, 38 | required: true, 39 | shortName: "t", 40 | longName: "test", 41 | description: "Test parameter", 42 | isFlag: false, 43 | defaultValue: "test content", 44 | }, 45 | ]; 46 | -------------------------------------------------------------------------------- /src/model/result.ts: -------------------------------------------------------------------------------- 1 | export interface ResultData { 2 | description: string; 3 | script: string; 4 | dialect: any; 5 | } 6 | -------------------------------------------------------------------------------- /src/templates.d.ts: -------------------------------------------------------------------------------- 1 | declare module "*.hbs"; 2 | -------------------------------------------------------------------------------- /src/templates/gnu.hbs: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Like this script? 3 | # Generate boilerplate for similar ones at https://bashplate.wolfgang-werner.net. 4 | 5 | set -o errexit # exit on error 6 | set -o nounset # don't allow unset variables 7 | # set -o xtrace # enable for debugging 8 | 9 | usage() { 10 | printf "{{description}}\n" 11 | 12 | printf "Usage: $(basename "$0") " 13 | {{#each options}} 14 | {{#with this}} 15 | printf -- "{{#unless required}}[{{/unless}}-{{shortName}}{{#if longName}}, --{{longName}}{{/if}}{{#unless isFlag}}=< {{longName}} >{{/unless}}{{#unless required}}]{{/unless}} " 16 | {{/with}} 17 | {{/each}} 18 | printf "\n" 19 | 20 | {{#each options}} 21 | {{#with this}} 22 | printf " -%s{{#if longName}}, --%s{{/if}}\t%s%s\n" "{{shortName}}" "{{longName}}" "{{description}}" "{{#if defaultValue}} (default: {{defaultValue}}){{/if}}" 23 | {{/with}} 24 | {{/each}} 25 | } 26 | 27 | version() { 28 | printf "0.0.1\n" 29 | } 30 | 31 | # default values 32 | {{#each flagOptions}} 33 | {{#with this}} 34 | opt_{{longName}}="false" 35 | {{/with}} 36 | {{/each}} 37 | {{#each parameterOptions}} 38 | {{#with this}} 39 | opt_{{longName}}="{{defaultValue}}" 40 | {{/with}} 41 | {{/each}} 42 | 43 | # declared functions 44 | {{#each callingOptions}} 45 | {{#with this}} 46 | {{functionCall}}() { 47 | printf "Called {{functionCall}}{{#if defaultValue}} with value $opt_{{longName}}{{/if}}\n" 48 | } 49 | {{/with}} 50 | {{/each}} 51 | 52 | # option parsing 53 | OPTSPEC=:{{#each options}}{{#with this}}{{shortName}}{{#unless isFlag}}:{{/unless}}{{/with}}{{/each}} 54 | OPTSPEC_LONG={{#each options}}{{#with this}}{{longName}}{{#unless isFlag}}:{{/unless}}{{/with}}{{#unless @last}},{{/unless}}{{/each}} 55 | 56 | SCRIPT=`basename $0` 57 | OPTS=`getopt --name "$SCRIPT" --options $OPTSPEC --long $OPTSPEC_LONG -- "$@"` 58 | 59 | while [ $# -gt 0 ]; do 60 | case "$1" in 61 | {{#each options}} 62 | {{#with this}} 63 | -{{shortName}} {{#if longName}}| --{{longName}}{{/if}}) {{#if functionCall}}{{functionCall}}; {{/if}}{{#unless isFlag}}opt_{{longName}}=$2; shift; {{else}}opt_{{longName}}="true"; {{/unless}} {{#if exitCode}}exit {{exitCode}} {{/if}} ;; 64 | {{/with}} 65 | {{/each}} 66 | --) shift; break ;; 67 | *) break ;; 68 | esac 69 | shift 70 | done 71 | 72 | {{#if requiredOptions}} 73 | # required option validation 74 | if {{#each requiredOptions}}{{#with this}}[ -z "$opt_{{longName}}" ]{{#unless @last}} || {{/unless}}{{/with}}{{/each}} ; then 75 | usage 76 | exit 1 77 | fi 78 | {{/if}} 79 | 80 | # convenience variables 81 | __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 82 | __file="${__dir}/$(basename "${BASH_SOURCE[0]}")" 83 | __base="$(basename ${__file} .sh)" 84 | __root="$(cd "$(dirname "${__dir}")" && pwd)" # update this to make it point your project's root 85 | 86 | 87 | # this would be a good place to start writing your actual script. 88 | echo "$__base was called with..." 89 | {{#each options}} 90 | {{#with this}} 91 | echo -{{shortName}}, --{{longName}}=$opt_{{longName}} 92 | {{/with}} 93 | {{/each}} 94 | -------------------------------------------------------------------------------- /src/templates/posix.hbs: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Like this script? 3 | # Generate boilerplate for similar ones at https://bashplate.wolfgang-werner.net. 4 | 5 | set -o errexit # exit on error 6 | set -o nounset # don't allow unset variables 7 | # set -o xtrace # enable for debugging 8 | 9 | usage() { 10 | printf "{{description}}\n" 11 | 12 | printf "Usage: $(basename "$0") " 13 | {{#each options}} 14 | {{#with this}} 15 | printf -- "{{#unless required}}[{{/unless}}-{{shortName}}{{#unless isFlag}}=< {{longName}} >{{/unless}}{{#unless required}}]{{/unless}} " 16 | {{/with}} 17 | {{/each}} 18 | printf "\n" 19 | 20 | {{#each options}} 21 | {{#with this}} 22 | printf " -%s\t%s - %s%s\n" "{{shortName}}" "{{longName}}" "{{description}}" "{{#if defaultValue}} (default: {{defaultValue}}){{/if}}" 23 | {{/with}} 24 | {{/each}} 25 | } 26 | 27 | version() { 28 | printf "0.0.1\n" 29 | } 30 | 31 | # default values 32 | {{#each flagOptions}} 33 | {{#with this}} 34 | opt_{{longName}}="false" 35 | {{/with}} 36 | {{/each}} 37 | {{#each parameterOptions}} 38 | {{#with this}} 39 | opt_{{longName}}="{{defaultValue}}" 40 | {{/with}} 41 | {{/each}} 42 | 43 | # declared functions 44 | {{#each callingOptions}} 45 | {{#with this}} 46 | {{functionCall}}() { 47 | printf "Called {{functionCall}}{{#if defaultValue}} with value $opt_{{longName}}{{/if}}\n" 48 | } 49 | {{/with}} 50 | {{/each}} 51 | 52 | # option parsing 53 | OPTSPEC=:{{#each options}}{{#with this}}{{shortName}}{{#unless isFlag}}:{{/unless}}{{/with}}{{/each}} 54 | while getopts $OPTSPEC option; do 55 | case "$option" in 56 | {{#each options}} 57 | {{#with this}} 58 | {{shortName}} ) {{#unless isFlag}}opt_{{longName}}=$OPTARG; {{else}}opt_{{longName}}="true"; {{/unless}}{{#if functionCall}}{{functionCall}}; {{/if}}{{#if exitCode}}exit {{exitCode}} {{/if}} ;; 59 | {{/with}} 60 | {{/each}} 61 | \? ) echo "Unknown option: -$OPTARG" >&2; exit 1;; 62 | : ) echo "Missing option argument for -$OPTARG" >&2; exit 1;; 63 | * ) echo "Unimplemented option: -$OPTARG" >&2; exit 1;; 64 | esac 65 | done 66 | shift $((OPTIND - 1)) 67 | 68 | {{#if requiredOptions}} 69 | # required option validation 70 | if {{#each requiredOptions}}{{#with this}}[ -z "$opt_{{longName}}" ]{{#unless @last}} || {{/unless}}{{/with}}{{/each}} ; then 71 | usage 72 | exit 1 73 | fi 74 | {{/if}} 75 | 76 | # convenience variables 77 | __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 78 | __file="${__dir}/$(basename "${BASH_SOURCE[0]}")" 79 | __base="$(basename ${__file} .sh)" 80 | __root="$(cd "$(dirname "${__dir}")" && pwd)" # update this to make it point your project's root 81 | 82 | 83 | # this would be a good place to start writing your actual script. 84 | echo "$__base was called with..." 85 | {{#each options}} 86 | {{#with this}} 87 | echo -{{shortName}}=$opt_{{longName}} 88 | {{/with}} 89 | {{/each}} 90 | 91 | -------------------------------------------------------------------------------- /src/views/About.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react"; 2 | import { NavLink } from "react-router-dom"; 3 | 4 | export const About = () => ( 5 |
6 |
7 |
8 |
9 |
10 |

What

11 |

... is this thing for?

12 | Bash Plate lets you generate boilerplate for shell scripts that 13 | takes care of receiving, parsing, validating and documenting script 14 | arguments. 15 |
16 | It is intended to give you a starting point for your scripts that is 17 | reasonably clean without you having to remember the different 18 | possibilities and their details for receiving script arguments. 19 |
20 |
21 | It is not intended to be used in a round-trip fashion, although you 22 | could swap out the generated portion in your file with a newly 23 | generated one adding more options. But generally, your on your own 24 | after generation. 25 |
26 | It kinda works on a reasonable recent phone in landscape mode, but 27 | if you want to generate shell scripts on your mobile on the go, you 28 | should really have you checked out. 29 |
30 |
31 | 32 | Go generate! 33 | 34 |

35 | Written by this human. 36 |

37 |
38 |       39 |
40 |

Why

41 |

... does it even exist?

42 | Each time I need to write a shell script - which is 43 | exactly 44 | {" "} 45 | long enough to forget all the details after writing the last one - , 46 | I go through a process of thought like the following: 47 |
    48 |
  • 49 | 🧐 "I want this to be reusable, so I'll use proper 50 | options & help messages" 51 |
  • 52 |
  • 53 | 🤔 "Hmmm, was it getopt or getopts? Which was the GNU and 54 | which the POSIX one? Shouldn't I use argbash?" 55 |
  • 56 |
  • 57 | 😤 "So how does the option spec need to look like? Why is 58 | that colon at the start again? Does the following option take an 59 | argument?" 60 |
  • 61 |
  • 62 | 😩 "How to I test if an argument was set? Reliably and and 63 | in a portable manner?" 64 |
  • 65 |
66 |
67 | Now, I either: 68 |
    69 |
  • 70 | Copy an old script and fiddle around in it - bad, since there is 71 | stuff to remove, I could break things and I still need to think 72 |
  • 73 |
  • 74 | Read up on option parsing - bad, as it takes time and I have to 75 | decide between getopt, getopts and argbash 76 |
  • 77 |
  • 78 | Just skip parameterizing the script and hard code everything - 79 | bad, exactly what I started out 80 | not to do 81 |
  • 82 |
83 |
84 | So basically, I try to hack myself into writing better scripts. 85 |
86 | If you can relate, give it a spin. 87 |
88 |
89 | 90 |
91 |
92 |

More

93 |

94 | Some useful references, alternatives and background. 95 |

96 | 159 |
160 |
161 |
162 |
163 |
164 | ); 165 | -------------------------------------------------------------------------------- /src/views/Generator.tsx: -------------------------------------------------------------------------------- 1 | import React, { ChangeEvent } from "react"; 2 | import { Result, Options } from "~components"; 3 | import { OptionData, ResultData } from "~model"; 4 | 5 | type GeneratorProps = { 6 | scriptDescription: string; 7 | options: OptionData[]; 8 | result: ResultData; 9 | onChangeDescription: (description: string) => void; 10 | onAddOption: () => void; 11 | onChangeOption: (option: OptionData) => void; 12 | onRemoveOption: (option: OptionData) => void; 13 | onCopy: () => void; 14 | onSave: () => void; 15 | onCreateGist: () => void; 16 | onChangeDialect: any; 17 | }; 18 | 19 | export const Generator = ({ 20 | scriptDescription, 21 | options, 22 | result, 23 | onChangeDescription, 24 | onAddOption, 25 | onChangeOption, 26 | onRemoveOption, 27 | onCopy, 28 | onSave, 29 | onCreateGist, 30 | onChangeDialect, 31 | }: GeneratorProps) => { 32 | const handleDescriptionChange = (e: ChangeEvent) => { 33 | onChangeDescription(e.currentTarget.value); 34 | }; 35 | 36 | return ( 37 |
38 |

39 | Bash Plate lets you generate boilerplate for shell scripts that takes 40 | care of receiving, parsing, validating and documenting script arguments. 41 |

42 |
43 |
44 | 51 |
52 |
53 | 59 | 60 | 67 |
68 | ); 69 | }; 70 | -------------------------------------------------------------------------------- /src/views/index.tsx: -------------------------------------------------------------------------------- 1 | export * from "./About"; 2 | export * from "./Generator"; 3 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "dom", 6 | "dom.iterable", 7 | "esnext" 8 | ], 9 | "allowJs": true, 10 | "skipLibCheck": true, 11 | "esModuleInterop": true, 12 | "allowSyntheticDefaultImports": true, 13 | "strict": true, 14 | "forceConsistentCasingInFileNames": true, 15 | "module": "es2015", 16 | "jsx": "react", 17 | "moduleResolution": "node", 18 | "resolveJsonModule": true, 19 | "isolatedModules": true, 20 | "noEmit": true, 21 | "baseUrl": "./src", 22 | "paths": { 23 | "~*": ["./*"] 24 | }, 25 | }, 26 | "include": [ 27 | "src/**/*" 28 | ] 29 | } --------------------------------------------------------------------------------