29.01.2014 Views

A CIL Tutorial - Department of Computer Science - ETH Zürich

A CIL Tutorial - Department of Computer Science - ETH Zürich

A CIL Tutorial - Department of Computer Science - ETH Zürich

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 14. IMPLEMENTING A SIMPLE DSL 124<br />

../test/tut14.c<br />

argument(int, boolarg) {<br />

.short_form = "b",<br />

.help_text = "A boolean argument",<br />

};<br />

The program also accepts an argument called intarg that species a particular integer. Setting the<br />

has opt eld to ARG HAS OPT indicates that the argument takes an option, which is parsed using<br />

the given format specier. It can be passed with --intarg n or -i n, and then the value n will be<br />

accessable in the program through the variable intarg. We have also stated a requirement that the<br />

given number must bigger than 0.<br />

../test/tut14.c<br />

argument(int, intarg, mandatory) {<br />

.short_form = "i",<br />

.help_text = "An integer argument (>0)",<br />

.format = "%d",<br />

.requires = arg_assert(intarg > 0),<br />

.has_opt = ARG_HAS_OPT,<br />

};<br />

int main(int argc, char *argv[])<br />

{<br />

printf("%d %d\n", boolarg, intarg);<br />

return 0;<br />

}<br />

This example can be compiled by saying:<br />

$ ciltutcc --enable-tut14 -o tut14 test/tut14.c<br />

Then we can run tut14 and supply the correct arguments:<br />

$ ./tut14 -i 4 --boolarg<br />

1 4<br />

But now let's see what happens when we get it wrong:<br />

$ ../tut14 --intarg -5<br />

Improper arguemnts<br />

-b,boolarg A boolean argument (0)<br />

-i,intarg An integer argument (>0) (mandatory)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!