ros / xacro

Xacro is an XML macro language. With xacro, you can construct shorter and more readable XML files by using macros that expand to larger XML expressions.
http://www.ros.org/wiki/xacro
BSD 3-Clause "New" or "Revised" License
88 stars 98 forks source link

Xacro file argument introspection #345

Open reinzor opened 5 months ago

reinzor commented 5 months ago

We would like to add a feature that allows a user to introspect xacro files. This way, we can query what arguments are available and prompt these to the user. We could also add additional validation to the defined arguments and some documentation. What are your thoughts about this? An example:

<xacro:arg name="argument1" default="value1" doc="Argument 1 doc" choices="value1, value2, value3" />
xacro --args test.xacro
arguments:
  - name: argument 1
  - default: value1
  - choices: [value1, value2, value3]
  - doc: Argument 1 doc
rhaschke commented 5 months ago

This is generally a good idea. However, as of now, arguments don't have a choices attribute yet and I'm not sure it makes sense to introduce it. Often, arguments can take arbitrary values, such that a choices restriction doesn't make sense. Another restriction option is a range of numbers, which is difficult to express as a discrete list of choices. In both cases, the allowed parameters could be easily specified within the doc string. Of course, this makes generic validation impossible, but that's impossible anyway given the multitude of possible constraints. Note that validation is possible with corresponding code already. Regarding the output formatting, I'd prefer a more compact format, like:

arguments:
  - argument1=default: doc
  - ...
reinzor commented 5 months ago

Thanks; I agree with your comment on the choices attribute. I will try to prepare a PR that adds the argument introspection and the doc attribute.

reinzor commented 5 months ago

Another thought that comes to mind, couldn't we add a validation attribute that accepts an expression as defined here: https://github.com/ros/xacro/wiki#expression-evaluation- . would this work? For example:

<arg name="arg1" validation="${xacro.arg('arg1') in ['value1', 'value2']}" />
rhaschke commented 5 months ago

This would be more generic at least.