Skip to main content
Deno 2 is finally here ๐ŸŽ‰๏ธ
Learn more
Module

x/denops_std/argument/mod.ts

๐Ÿ“š Standard module for denops.vim
Latest
import * as denopsStd from "https://deno.land/x/denops_std@v6.5.1/argument/mod.ts";

A module to handle Vim's command arguments like the followings.

:MyCommand ++enc=sjis ++ff=dos -f --foo=foo --bar=bar --bar=baz hello world

Developers should utilize [<f-args>] in Vim script to acquire the arguments as a string array, adhering to the standard Vim command behavior.

For example:

command! -nargs=* MyCommand call denops#request("myplugin", "test", [[<f-args>]])

Then, developers can use this module to parse, validate, or format the arguments.

import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.1/mod.ts";
import {
  builtinOpts,
  formatFlags,
  formatOpts,
  parse,
  validateFlags,
  validateOpts,
} from "https://deno.land/x/denops_std@v6.5.1/argument/mod.ts";

export const main: Entrypoint = async (denops) => {
  denops.dispatcher = {
    test(args: unknown) {
      // Parse string array to extract `opts`, `flags`.
      const [opts, flags, residues] = parse(args as string[]);

      console.log(opts);
      // { "enc": "sjis", "ff": "dos" }
      console.log(flags);
      // { "f": "", "foo": "foo", "bar": ["bar", "baz"] }
      console.log(residues);
      // ["hello", "world"]

      // Validate if `opts` has unknown options
      validateOpts(opts, ["enc", "ff"]);

      // Or use `builtinOpts` to validate Vim's builtin `++opts`
      validateOpts(opts, builtinOpts);

      // Validate if `flags` has unknown flags
      validateFlags(flags, ["f", "foo", "bar"]);

      // Reformat `opts` to string
      console.log(formatOpts(opts));
      // "++enc=sjis ++ff=dos"

      // Reformat `flags` to string
      console.log(formatFlags(flags));
      // "-f --foo=foo --bar=bar --bar=baz"
    },
  };
}

Variables

List of Vim builtin options (++opts)

Functions

Format key and value to construct string array.

Format flags to construct string array.

Format key and value to construct string array.

Format opts to construct string array.

Parse string array to extract opts, flags.

Parse string array to extract flags (-f/--flag).

Parse string array to extract opts (++opt).

Validate if flags has unknown attributes.

Validate if opts has unknown attributes.