How to prevent front end from suggesting Symbol option names (when actually String), and from adding them to Global` context

You can use undocumented "OptionNames" property of SyntaxInformation to customize option names suggestions (and coloring of invalid option names).

MyFunc // ClearAll
MyFunc // Options = {"MyOpt" -> True};
MyFunc // SyntaxInformation = {
    "ArgumentsPattern" -> {_, OptionsPattern[]},
    "OptionNames" -> {"\"MyOpt\""}
};

After typing MyFunc[3.2, My I get (in version 11.0):

Auto-completion suggestions screen-shot


By convention option names are Symbols, and String equivalents are accepted.

For example all the formal Options of Plot are Symbols:

Head /@ First /@ Options[Plot] // Tally
{{Symbol, 60}}

Yet you can give options as Strings as well:

Plot[x^2, {x, 1, 10}, "PlotStyle" -> Red, "Frame" -> True,
  "Axes" -> False, "PlotLabel" -> "This is a plot"]

enter image description here

What you want therefore appears to go against the intentional design of Mathematica.

You may need to specifically interfere with that design to achieve your goal. I thought it would be possible to do this by modifying Front End function FE`OC but changing that doesn't seem to have an effect; I suspect a different function is now used to populate these menus, but I cannot find it.


With modification of FE`OC not working as hoped, and remembering Prompt a set of possible options it occurred to me that pre-escaping a String option name might work, and indeed it does! Well, sort of.

Options[MyFunc] = {"\"MyOpt\"" -> True};

SyntaxInformation[MyFunc] = {"ArgumentsPattern" -> {_, OptionsPattern[]}};

MyFunc[x_, OptionsPattern[]] := {x, OptionValue["MyOpt"]}

MyFunc[foo, MyO]  (* while typing in MyO *)

enter image description here

Selecting this does actually produce a String option name. However running it give an error:

MyFunc[foo, "MyOpt" -> bar]

OptionValue::nodef: Unknown option MyOpt for MyFunc. >>

{foo, bar}

So OptionValue doesn't like this, but it still extracts the option value correctly. It does not however handle the default case:

MyFunc[foo]

OptionValue::optnf: Option name MyOpt not found in defaults for MyFunc. >>

{foo, "MyOpt"}

I think therefore that you may need to avoid OptionValue and write your own filter.

You will also need to deal with adding or modifying the Options list, as

SetOptions[MyFunc, "MyOpt" -> False]

SetOptions::optnf: MyOpt is not a known option for MyFunc. >>

Before you pursue this further I think you should consider if you really have good reason to deviate from the existing design of Mathematica and all the headache that might entail.


Would modifying the list of Symbols returned by ? be of any use to use? See e.g.

  • How to sort the functions in a Package?