Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body class="bg-gray-200 flex items-center justify-center h-screen">
  <div class="rounded-lg shadow-md bg-white text-gray-700 p-6">
    <h1 class="text-xl font-semibold leading-loose tracking-wide">Extendable command parser</h1>
    <p class="text-lg">Add command templates to the commands array or try the echo and sum commands.<p>

    <div class="py-4">
      Output:
      <div id="output">
        type a command into the input field in order to get parsed data
       </div>
      </div>
      <form id="inputform">
        <input type="text" id="rawinput" class="border rounded border-gray-500 mr-2 p-2" />
        <button type="submit" class="bg-green-300 py-2 px-4 rounded-lg">Parse</button>
      </form>
  </div>
 </body>
              
            
!

CSS

              
                
              
            
!

JS

              
                var argumentTypes = [
  {
    type: "string",
    replace: "([a-z]+)",
    transform: (arg) => {
      return arg
    }
  },
  {
    type: "number",
    replace: "([0-9]+)",
    transform: (arg) => {
      return parseInt(arg)
    }
  }
]

var commands = [
  {
    name: "echo",
    scheme: "echo $input",
    args:[
      {
        name: "input",
        type: "string"
      }
    ],
    handler: ({input}) => {
      return input;
    }
  },
  {
    name: "sum",
    scheme: "sum $val1 $val2",
    args:[
      {
        name: "val1",
        type: "number"
      },
      {
        name: "val2",
        type: "number"
      }
    ],
    handler: ({val1, val2}) => {
      return val1 + val2;
    }
  }
];

var cmd = (input) => {
  //check all commands
  for(let c of commands){
    var reg = c.scheme
    for(let arg of c.args){
      var argumentType = argumentTypes.find(a => a.type === arg.type);
      if(argumentType == undefined){
        console.log("unsupported argumenttype");
        return;
      }
      reg = reg.replace("$" + arg.name, argumentType.replace)
    }
    var regExp = new RegExp(reg)
    var match = input.match(regExp)
    if(match){
      match.shift()
      var paramObj = {}
      for(var i = 0; i < c.args.length; i++){
        var argumentType = argumentTypes.find(a => a.type === c.args[i].type);
        paramObj[c.args[i].name] = argumentType.transform(match[i])
      }
      return c.handler(paramObj)
    }
  }
  console.log("no matching command found")
}







document.getElementById('inputform').onsubmit = function (evt) { 	
  evt.preventDefault();
   let input = document.getElementById('rawinput').value;
  if(input !== ""){ 
    var parsed = cmd(input);
    if(parsed == undefined)
      parsed = "Unable to parse command";
    document.getElementById("output").innerHTML = parsed;
    document.getElementById('rawinput').value = "";
  }
}
              
            
!
999px

Console