In some of my commands, I want to allow the user (which is often me) to select from a list of user defined strings, often these strings are the names of items defined elsewhere in my plugin. The AddOptionList
method is often useful for this. However, there are certain prohibited characters that silently prevent certain strings from being displayed.
In the below code, when the user selects “ItemName” in the command Value1 and Value2 will be displayed, but Value2.1 will not (presumably because of the presence of a period in the string). What are the rules in displayable values? It might be nice (developer friendly) to throw an error if one or more values do not meet these rules. The same rules apply to Option names themselves. For example, if the developer were to define and option named “Item Name” it will be silently discarded (because of the space). In this case in particular I think an Exception should probably be raised, as this is a fairly subtle thing that is all syntactically correct.
GetOption go = new GetOption();
go.AcceptNothing(true);
List<string> myvalues = new List<string>(){"Value1", "Value2", "Value2.1"}
int initialSelection = 0
int optionIndex = go.AddOptionList("ItemName", myvalues, initialSelection);
go.get()
Thanks!
Hi @bradlcampbell, you might check your option strings using either IsValidOptionName or IsValidOptionValueName. I think in general spaces inside option names and number only strings are not allowed.
_
c.
Thanks, Clement! I was not aware of those methods. However, I’m not sure that the IsValidOptionValueName method (or my understanding of where it applies) is correct. Here is some python code I put together to play with this:
from Rhino.Input.Custom import CommandLineOption, GetOption
# list of strings to test
test_names = [
"a",
"a1",
"a1.1",
"/a",
"a/b",
"a.b",
"a_b",
"a b",
"!a",
"a!"
]
safe_names = [tn for tn in test_names if CommandLineOption.IsValidOptionValueName(tn)]
# These names should be allowed in AddOptionList if I understand things correctly
print(safe_names)
# This prints:
# ['a', 'a1', 'a1.1', 'a/b', 'a.b', 'a_b', 'a!']
##############################################
# Print a table of which strings should be allowed as option names and value names
print("{0: <12}{1: <12}{2: <16}".format("String","ValidName?", "ValidOptionName?"))
for tn in test_names:
validName = CommandLineOption.IsValidOptionName(tn)
validOptName = CommandLineOption.IsValidOptionValueName(tn)
print("{0: <12}{1: <12}{2: <16}".format(tn, str(validName), str(validOptName)))
# This prints:
# String ValidName? ValidOptionName?
# a True True
# a1 True True
# a1.1 False True
# /a False False
# a/b False True
# a.b False True
# a_b True True
# a b False False
# !a False False
# a! False True
##############################################
#Proof if in the pudding - try it out
go = GetOption()
go.SetCommandPrompt("Let us see")
currentIndex = 0
optIndex = go.AddOptionList("WackyThings", safe_names, currentIndex)
go.Get()
# The result is this command prompt
# Let us see ( WackyThings=a ): WackyThings
# WackyThings <a> ( a a1 a_b )
It looks like AddOptionList values have the same rules as OptionNames.
@dale, @stevebaer,
Is this the expected behavior?
Thanks,
Brad