3 @c user.texi --- User manual
5 @c Copyright (C) 2010 Didier Verna
7 @c Author: Didier Verna <didier@lrde.epita.fr>
8 @c Maintainer: Didier Verna <didier@lrde.epita.fr>
9 @c Created: Fri Jul 2 02:24:49 2010
10 @c Last Revision: Mon Nov 1 16:08:21 2010
12 @c This file is part of Clon.
14 @c Clon is free software; you can redistribute it and/or modify
15 @c it under the terms of the GNU General Public License version 3,
16 @c as published by the Free Software Foundation.
18 @c Clon is distributed in the hope that it will be useful,
19 @c but WITHOUT ANY WARRANTY; without even the implied warranty of
20 @c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 @c GNU General Public License for more details.
23 @c You should have received a copy of the GNU General Public License
24 @c along with this program; if not, write to the Free Software
25 @c Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 @c Contents management by FCM version 0.1.
33 @c ====================================================================
35 @c ====================================================================
37 @setfilename clon-user.info
38 @settitle The Clon User Manual
44 @c ====================================================================
46 @c ====================================================================
47 @set VERSION 1.0 beta 10 "Michael Brecker"
48 @set COPYRIGHT_DATE 2010
49 @setchapternewpage odd
50 @setcontentsaftertitlepage
52 The Clon User Manual for version @value{VERSION}.
53 @end documentdescription
57 @c ====================================================================
59 @c ====================================================================
98 @c --------------------------------
99 @c Manuals cross-referencing macros
100 @c --------------------------------
101 @c The following 3 macros are here to circumvent the info filenames
102 @c changes, when referencing the end-user manual:
104 @macro pxenduserref{node}
106 @pxref{\node\, , , clon-enduser, The Clon End-User Manual}
109 @pxref{\node\, , , enduser, The Clon End-User Manual}
113 @macro xenduserref{node}
115 @xref{\node\, , , clon-enduser, The Clon End-User Manual}
118 @xref{\node\, , , enduser, The Clon End-User Manual}
122 @macro enduserref{node}
124 @ref{\node\, , , clon-enduser, The Clon End-User Manual}
127 @ref{\node\, , , enduser, The Clon End-User Manual}
141 @cindex @t{--clon-\opt\}
142 @cindex Built-In Options, @t{--clon-\opt\}
143 @cindex Options, built-in, @t{--clon-\opt\}
146 @c Common option properties
147 @macro copindex{prop}
148 @cindex Options, common properties, @t{\prop\}
149 @cindex Common Option Properties, @t{\prop\}
153 @c #### NOTE: not an alias because of argument syntax.
154 @macro oindex{name,class}
155 @cindex \name\ (@t{\class\})
156 @cindex Options, \name\ (@t{\class\})
161 @macro voindex{name,class}
162 @cindex \name\ (@t{\class\})
163 @cindex Valued Options, \name\ (@t{\class\})
164 @cindex Options, valued, \name\ (@t{\class\})
168 @c Common valued option properties
169 @macro cvopindex{prop}
170 @cindex Valued Options, common properties, @t{\prop\}
171 @cindex Common Valued Option Properties, @t{\prop\}
172 @cindex Options, valued, common properties, @t{\prop\}
175 @c Valued option properties
176 @macro vopindex{name,class,prop}
177 @cindex \name\ (@t{\class\}), properties, @t{\prop\}
178 @cindex Valued Options, \name\ (@t{\class\}), properties, @t{\prop\}
179 @cindex Options, valued, \name\ (@t{\class\}), properties, @t{\prop\}
186 @macro foindex{func,opt}
187 @findex \func\@r{, options, }\opt\
191 @macro fiindex{func,item}
192 @findex \func\@r{, items, }\item\
196 @macro fkindex{func,key}
197 @findex \func\@r{, keys, }\key\
201 @macro fiaindex{func,arg}
202 @findex \func\@r{, initargs, }\arg\
205 @c Generic functions sub-index
206 @macro gfsubindex{func}
207 @findex @r{Generic Functions, }\func\
216 @c Generic function methods
217 @macro gfmindex{func,method}
218 @findex \func\@r{, methods, }\method\
219 @findex @r{Generic Functions, }\func\@r{, methods, }\method\
222 @c #### FIXME: I need this because when using @defun, the function is
223 @c referenced directly but not under the Constructors sub-index. This
224 @c sucks because what I would like is the ability to define my own
225 @c @defXXX macros, which apparently I can't. Besides, doing this
226 @c produces 2 separate index entries, instead of only one with two pagerefs.
227 @c This problem stands for @gfsubindex (see above), @rfsubindex (see
228 @c below) and @ecsubindex (see below) as well.
229 @c Constructor functions sub-index
230 @macro cfsubindex{func}
231 @findex @r{Constructors, }\func\
234 @c Constructor functions
240 @c Constructor function initargs
241 @macro cfiaindex{func,arg}
242 @fiaindex{\func\, \arg\}
243 @findex @r{Constructors, }\func\@r{, initargs, }\arg\
246 @c Reader functions subindex
247 @macro rfsubindex{func}
248 @findex @r{Readers, }\func\
261 @c Environment variables
264 @vindex @r{Environment, }\var\
274 @tpindex @r{Classes, }\cls\
278 @macro clssindex{cls,slot}
279 @tpindex \cls\@r{, slots, }\slot\
280 @tpindex @r{Classes, }\cls\@r{, slots, }\slot\
284 @macro clsiaindex{cls,arg}
285 @tpindex \cls\@r{, initargs, }\arg\
286 @tpindex @r{Classes, }\cls\@r{, initargs, }\arg\
292 @tpindex @r{Option Classes, }\cls\
295 @c Error conditions subindex
296 @macro ecsubindex{cond}
297 @tpindex @r{Error Conditions, }\cond\
306 @c Error condition slots
307 @macro ecsindex{cond,slot}
308 @tpindex \cond\@r{, slots, }\slot\
309 @tpindex @r{Error Conditions, }\cond\@r{, slots, }\slot\
313 @macro pkgindex{name}
315 @tpindex @r{Packages, }\name\
320 @c ====================================================================
321 @c Info Category and Directory
322 @c ====================================================================
323 @dircategory Common Lisp
325 * Clon User: (clon-user). The Clon User Manual.
330 @c ====================================================================
332 @c ====================================================================
335 Copyright @copyright{} @value{COPYRIGHT_DATE} Didier Verna.
337 Permission is granted to make and distribute verbatim copies of this
338 manual provided the copyright notice and this permission notice are
339 preserved on all copies.
342 Permission is granted to process this file through TeX and print the
343 results, provided the printed document carries a copying permission
344 notice identical to this one except for the removal of this paragraph
345 (this paragraph not being relevant to the printed manual).
348 Permission is granted to copy and distribute modified versions of this
349 manual under the conditions for verbatim copying, provided also that the
350 sections entitled ``Copying'' and ``GNU General Public License'' are
351 included exactly as in the original, and provided that the entire
352 resulting derived work is distributed under the terms of a permission
353 notice identical to this one.
355 Permission is granted to copy and distribute translations of this manual
356 into another language, under the above conditions for modified versions,
357 except that this permission notice may be stated in a translation
358 approved by the Free Software Foundation.
364 @c ====================================================================
366 @c ====================================================================
368 @title The Clon User Manual
369 @subtitle The @CmdLine{} Options Nuker, Version @value{VERSION}
372 @author Didier Verna <@email{didier@@lrde.epita.fr}>
374 @vskip 0pt plus 1filll
377 @hfill Cover art by Alexis Angelidis.
382 @c ====================================================================
384 @c ====================================================================
389 @c ====================================================================
391 @c ====================================================================
393 @node Top, Copying, (dir), (dir)
394 @top The Clon User Manual
396 This is the @clon{} User Manual for @clon{} version @value{VERSION}:
397 it explains how to use @clon{} in your application.
400 * Copying:: The GNU General Public License
401 * Introduction:: What Clon is all about
402 * Quick Start:: For the brave and the impatient
403 * Using Clon:: Clonificating your application
404 * Extending Clon:: Creating your own option types
405 * Advanced Usage:: Things rarely needed
406 * Conclusion:: That's all folks
407 * Quick Start Program:: The complete listing
408 * API Quick Reference:: The Complete protocols
409 * Indexes:: Concept, Function and Variable
417 @c ====================================================================
419 @c ====================================================================
420 @node Copying, Introduction, Top, Top
424 @clon{} is free software; you can redistribute it and/or modify it under
425 the terms of the GNU General Public License version 3, as published by
426 the Software Foundation.
428 @clon{} is distributed in the hope that it will be useful, but WITHOUT
429 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
430 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
433 You should have received a copy of the GNU General Public License along
434 with this program; if not, write to the Free Software Foundation, Inc.,
435 675 Mass Ave, Cambridge, MA 02139, USA.
440 @c ====================================================================
442 @c ====================================================================
443 @node Introduction, Quick Start, Copying, Top
444 @chapter Introduction
446 @clon{} is a library for managing @cmdline{} options in standalone @cl{}
447 applications. It provides a unified option syntax with both short and
448 long names, automatic completion of partial names and automatic
449 retrieval/conversion of option arguments from the @cmdline{}, associated
450 environment variables, fallback or default values. @clon{} comes with a
451 set of extensible option types (switches, paths, strings @etc{}).
452 @clon{} also provides automatic generation and formatting of help
453 strings, with support for highlighting on @tty{}'s through ISO/IEC 6429
454 SGR. This formatting is customizable through @emph{themes}.
456 Depending on the target audience, @clon{} stands for either ``The
457 @CmdLine{} Options Nuker'' or ``The @cl{} Options Nuker''. @clon{} also
458 has a recursive acronym: ``@clon{} Likes Options Nuking'', and a reverse
459 one: ``Never Omit to Link with @clon{}''. Other possible expansions of
460 the acronym are still being investigated.
462 This manual is for the @clon{} @emph{user}, that is, the developer of a
463 @cl{} application who wants to use @clon{} for @cmdline{} option
464 management@footnote{An application using @clon{} for its @cmdline{}
465 option management is said to be @emph{clonified}. It is also possible to
466 say @emph{clonfiscated}. However, we advise against using
467 @emph{clonistified}. The term @emph{clonificated} is also considered bad
468 style, and the use of @emph{clonificationated} is strictly prohibited.}.
469 As opposed to the user of the @emph{library}, the user of an
470 @emph{application} powered by @clon{} is called an @emph{end-user}.
471 @clon{} provides both a user manual (this one) and an end-user manual
472 (@pxenduserref{Top}). Everybody should read the end-user manual first.
474 @ref{Quick Start} provides a very short example in order to give an
475 overview of what's coming next. @ref{Using Clon} explains in detail how
476 to clonify your application, and @ref{Extending Clon} describe how to
477 extend @clon{} with your own option types.
481 @c ====================================================================
483 @c ====================================================================
484 @node Quick Start, Using Clon, Introduction, Top
487 In this chapter, we quickly build a very short program to get you
488 started. We assume that you have properly installed @clon{} for ASDF.
489 Since several bits are compiler-specific, we also assume that SBCL is
490 used in this example. If you want to try this program for real, the full
491 listing is available for cut'n paste in @ref{Quick Start Program}. For a
492 slightly longer example, have a look at the demonstration program called
493 @file{simple} in the distribution.
495 @pkgindex{com.dvlsoft.clon}
496 Let's put us in the @cl{} user package, and load @clon{} first. @clon{}
497 lives in a package named @samp{com.dvlsoft.clon}.
501 (in-package :cl-user)
504 (asdf:operate 'asdf:load-op :com.dvlsoft.clon)
505 (use-package :com.dvlsoft.clon)
509 In fact, using the @clon{} package directly is done here for simplicity,
510 but is not recommended. In case you find the package name too long to
511 prefix every symbol with, @clon{} provides a utility function that
512 allows you to add (and use) a shorter nickname instead (the
513 demonstration programs in the distribution show how to use it):
515 @cindex Package, nicknames
516 @defun nickname-package &optional NICKNAME
517 Add NICKNAME (:CLON by default) to the :COM.DVLSOFT.CLON package.
523 @cindex Text, in synopsis
524 @cindex Options, in synopsis
525 @cindex Groups, in synopsis
527 The next thing you want to do is to create a set of options, groups or
528 texts that your application will recognize and/or display in its help
529 string. The structure of your @cmdline{} is known to @clon{} as the
530 @dfn{synopsis}. In order to create it, use the macro @code{defsynopsis}.
532 @foindex{defsynopsis,:postfix}
533 @fiindex{defsynopsis,text}
534 @fiindex{defsynopsis,options}
535 @fiindex{defsynopsis,group}
538 (defsynopsis (:postfix "FILES...")
539 (text :contents "A very short program.")
540 (group (:header "Immediate exit options:")
541 (flag :short-name "h" :long-name "help"
542 :description "Print this help and exit.")
543 (flag :short-name "v" :long-name "version"
544 :description "Print version number and exit.")))
548 Note that the synopsis has a double role: it not only serves to define
549 the options recognized by your application, but also the order in which
550 every element appears in the help string. In that particular example, we
551 define a line of text and a group with a header and two flags.
554 @cfindex{make-context}
555 Now, we are going to define a function @code{main} for our standalone
556 application. The first thing that we need to do is create a
557 @dfn{context}. A context is an object that @clon{} uses to store various
558 things, including some state related to the parsing of the @cmdline{}.
559 You create a context with the function @code{make-context}.
564 "Entry point for our standalone application."
569 @cindex Options Retrieval, explicit
570 @cindex Retrieval, of options, explicit
572 @cindex Help String, display
574 At that point, @clon{} is ready to retrieve the options you want from
575 the @cmdline{}. Let's first see if the user has called the option
576 @option{-h}, and give him the help string. Option retrieval is done with
577 the function @code{getopt}, and automatic help string output with the
578 function @code{help}. Note that what we do here is @emph{not} process
579 the @cmdline{} in sequence, but look directly for a specific option by
580 name (this retrieval method is said to be @dfn{explicit}).
582 @fkindex{getopt,:short-name}
585 (when (getopt :short-name "h")
592 The @code{exit} function is a wrapper around an
593 implementation-dependent way to exit (shall I say quit?) the @cl{}
594 environment, hence the program. It takes an optional argument that
595 stands for the exit status.
597 @cindex Options Retrieval, sequential
598 @cindex Retrieval, of options, sequential
599 @findex do-cmdline-options
600 And now, we are going to retrieve the other options and simply print
601 them. This time however, we process the @cmdline{} sequentially (so this
602 retrieval method is said to be @dfn{sequential}). This is done with the
603 @code{do-cmdline-options} macro. We also close the @code{main} function.
607 (do-cmdline-options (option name value source)
608 (print (list option name value source)))
614 Finally, time to save the Lisp image.
623 The @code{dump} function is a wrapper around an implementation-dependent
624 way of creating standalone executables. The first argument is the name
625 of the executable to produce, and the second argument is the function to
628 We're now ready to compile this industrial-scale application and deliver
632 CC=gcc sbcl --script test.lisp
637 Err, by the way, you shoudn't use @option{--script} because it disables
638 the debugger, which is not recommended (@pxref{Error Management}).
642 @c ====================================================================
644 @c ====================================================================
645 @node Using Clon, Extending Clon, Quick Start, Top
648 Using @clon{} in your application is a two stages process. In phase 1,
649 you create a @dfn{synopsis}, which is a description of your
650 application's @cmdline{} and a @dfn{context}, which describes this
651 specific execution of the program. In phase 2, you retrieve the option
652 values and possibly display help strings. Phase 1 is called the
653 @dfn{initialization phase}, while phase 2 is called the @dfn{runtime
657 * Synopsis Definition:: Describing your command-line
658 * Context Creation:: Instantiating your command-line
659 * Integrity Checks:: Verifying the Clon semantics
660 * Option Retrieval:: Getting the options values
661 * Help:: Generating the help string
665 @c -------------------
666 @c Synopsis Definition
667 @c -------------------
668 @node Synopsis Definition, Context Creation, Using Clon, Using Clon
669 @section Synopsis Definition
671 @cindex Initialization Phase
672 @cindex Phase, initialization
675 Step one of the @clon{} initialization phase consists in defining a
676 @dfn{synopsis}. A synopsis is essentially a description of your
677 application's @cmdline{}: it describes what are the available options,
678 whether your application has a postfix @etc{} The synopsis, however,
679 plays a second role: it also describes the contents of your
680 application's help string. When you create a synopsis, you describe the
681 @cmdline{} and the help string at the same time.
684 * Synopsis Items:: Defsynopsis and its contents
685 * Built-In Option Types:: The exhaustive list
686 * Advanced Synopsis Creation:: The non-declarative way
689 @node Synopsis Items, Built-In Option Types, Synopsis Definition, Synopsis Definition
690 @subsection Synopsis Items
691 Look again at the synopsis definition provided in @ref{Quick Start}.
695 (defsynopsis (:postfix "FILES...")
696 (text :contents "A very short program.")
697 (group (:header "Immediate exit options:")
698 (flag :short-name "h" :long-name "help"
699 :description "Print this help and exit.")
700 (flag :short-name "v" :long-name "version"
701 :description "Print version number and exit.")))
705 You define a synopsis with the @code{defsynopsis} macro.
707 @defmac defsynopsis ([OPTIONS@dots{}]) ITEMS@dots{}
708 Define a new synopsis and return it. @var{OPTIONS} are key/value pairs.
709 @var{ITEMS} are text, group or option descriptions.
711 The following @var{OPTIONS} are currently available.
715 @foindex{defsynopsis,:postfix}@c
716 @cindex Command-Line, remainder
717 @cindex Remainder, of command-line
718 A string which will appear at the end of the synopsis line in the help
719 string. When you provide a postfix, you also implicitely tell @clon{}
720 that your application accepts non-option arguments at the end of the
721 @cmdline{} (this is called the @dfn{remainder} of the @cmdline{}). See
722 @enduserref{Option Separator} for more information on the behavior of
723 @clon{} with respect to postfixes. Also, see @ref{Command-Line
724 Remainder} on how to access the @cmdline{} remainder.
728 We now examine the syntax for each possible @var{ITEM}.
730 * Text:: Adding arbitrary text
731 * Options:: Adding options
732 * Groups:: Adding groups
735 @node Text, Options, Synopsis Items, Synopsis Items
738 @cindex Text, in synopsis
739 @fiindex{defsynopsis,text}
740 In order to add arbitrary text to your help string, use the following
744 @t{(text} [@var{OPTIONS@dots{}}]@t{)}
746 @var{OPTIONS} are key/value pairs. The following @var{OPTIONS} are
750 @cindex Text, contents
751 The actual text string. Try to make proper sentences when adding
752 arbitrary text. You can use explicit newline characters in your text if
753 you really want to go next line, but in general, you should not worry
754 about the formatting because the themes are here to do so. In
755 particular, don't finish your text with a newline. This would break
756 potential theme specifications.
761 When non-@code{nil}, the text won't appear in the help string. Hidden
762 texts can still be displayed individually though (@pxref{Help}).
765 @node Options, Groups, Text, Synopsis Items
766 @subsubsection Options
768 @cindex Options, in synopsis
769 @fiindex{defsynopsis,options}
770 In order to add an option to your help string, you must provide a list
771 beginning with the option type and followed by key/value pairs
772 describing the option's properties. For instance, to add a flag with a
773 short name and a description, you could do this:
776 (flag :short-name "h" :description "Print this help and exit.")
779 Option properties vary depending on the option type. The exact list of
780 available option types, and the corresponding properties are described
781 in @ref{Built-In Option Types}.
783 @node Groups, , Options, Synopsis Items
784 @subsubsection Groups
786 @cindex Groups, in synopsis
787 @fiindex{defsynopsis,group}
788 In order to add a group to your help string, use the following form:
791 @t{(group} ([@var{OPTIONS@dots{}}]) @var{ITEMS@dots{}}@t{)}
793 @cindex Text, in groups
794 @cindex Options, in groups
795 @cindex Groups, in groups
796 @var{OPTIONS} are key/value pairs. @var{ITEMS} simply are arbitrary
797 text, option or sub-group descriptions as we've just seen.
799 The following @var{OPTIONS} are currently available.
803 @cindex Header, in group
804 @foindex{defgroup,:header}@c
805 A string which will be displayed above the group's contents in the help
806 string. The same formatting recommendations as for arbitrary text apply
809 @cindex Hidden Groups
810 @cindex Groups, hidden
811 @foindex{defgroup,:hidden}@c
813 @cindex Built-In Groups
814 @cindex Groups, built-in
816 When non-@code{nil}, the group won't appear in the help string. Hidden
817 groups can still be displayed individually though (@pxref{Help}). For
818 instance, the @clon{} built-in group is hidden in the regular help
819 string, but the @option{--clon-help} option still displays it
823 @node Built-In Option Types, Advanced Synopsis Creation, Synopsis Items, Synopsis Definition
824 @subsection Built-In Option Types
825 @cindex Built-In Option Types
826 @cindex Options, built-in types
827 @cindex Option Types, built-in
828 In this section, we will review all the built-in option types that
829 @clon{} provides, along with their corresponding properties. You can use
830 them directly in your synopsis description. For adding personal option
831 types to @clon{}, see @ref{New Option Types}.
834 * Common Properties:: For all options
835 * Flags:: Built-in options without arguments
836 * Common Valued Option Properties:: For all valued options
837 * Built-In Valued Options:: Built-in options with arguments
840 @node Common Properties, Flags, Built-In Option Types, Built-In Option Types
841 @subsubsection Common Properties
842 @cindex Options, common properties
843 @cindex Common Option Properties
845 All option types in @clon{}, including those you define yourself
846 (@pxref{New Option Types}), have a set of basic, common properties. Here
851 @copindex{:short-name}
852 The option's short name. A string or @code{nil}.
854 @copindex{:long-name}
855 The option's long name. A string or @code{nil}.
857 @copindex{:description}
858 The option's descriptive text. A string or @code{nil}. The same
859 formatting recommendations as for arbitrary text apply (@pxref{Text}).
862 The option's associated environment variable. A string or @code{nil}.
865 @cindex Hidden Options
866 @cindex Options, hidden
868 When non-@code{nil}, the option won't appear in the help string. Hidden
869 options can still be displayed individually though (@pxref{Help}).
872 Note that an option is required to have at least one name (either short
873 or long). Non-@code{nil} but empty names are also prohibited, and of
874 course, a short name cannot begin with a dash (otherwise, it would be
875 mistaken for a long name, but did I really need to mention this?).
877 @node Flags, Common Valued Option Properties, Common Properties, Built-In Option Types
880 In @clon{}, options that don't take any argument are of type
881 @code{flag}. These options don't provide additional properties on top of
882 the common set described in @ref{Common Properties}. All properties
883 default to @code{nil}.
885 @node Common Valued Option Properties, Built-In Valued Options, Flags, Built-In Option Types
886 @subsubsection Common Valued Option Properties
887 @cindex Valued Options
888 @cindex Options, valued
889 @clsindex{valued-option}
890 All non-flag options in @clon{} are said to be @dfn{valued}. All valued
891 options, including those you define yourself (@pxref{New Option Types}),
892 share a set of additional properties. Here is a list of them.
894 @cindex Valued Options, common properties
895 @cindex Common Valued Option Properties
896 @cindex Options, valued, common properties
899 @cvopindex{:argument-name}
900 The name of the option's argument, as it appears in the help string. It
901 defaults to @code{"ARG"}, so that for instance, a @samp{name} option
902 would appear like this: @samp{--name=ARG}.
904 @cvopindex{:argument-type}
905 The status of the argument. Possible values are @code{:required} (the
906 default) and @code{:mandatory} which are synonyms, or @code{:optional}.
907 @item :fallback-value
908 @cvopindex{:fallback-value}
909 @cindex Values, source, fallback
911 @cvopindex{:default-value}
912 @cindex Values, source, default
913 The option's fallback and default values. Remember that a fallback value
914 only makes sense when the argument is optional. Besides, also when the
915 argument is optional, you need to provide at least a fallback or a
916 default value (or both of course; see @enduserref{Value Sources}).
919 @node Built-In Valued Options, , Common Valued Option Properties, Built-In Option Types
920 @subsubsection Built-In Valued Options
921 @cindex Option Types, valued
922 @cindex Built-In Option Types, valued
923 @cindex Options, built-in types, valued
924 @cindex Option Types, built-in, valued
925 @clon{} currently defines 6 built-in valued option types. These option
926 types may change the default value for some common properties, and / or
927 provide additional properties of their own. All of this is described
932 @voindex{Strings,stropt}@c
933 @cvopindex{:argument-name}@c
934 This option type is for options taking strings as their argument. String
935 options don't provide any additional properties, but their default
936 argument name is changed from @code{"ARG"} to @code{"STR"}.
938 @voindex{Lisp Objects,lispobj}@c
939 @cvopindex{:argument-name}@c
940 @vopindex{Lisp Objects,lispobj,:typespec}@c
941 @bioindex{line-width}@c
942 This option type is for options taking any kind of Lisp object as their
943 argument. @code{lispobj} options change their default argument name from
944 @code{"ARG"} to @code{"OBJ"}. Also, they provide an additional property
945 called @code{:typespec} which must be a @cl{} type specifier that the
946 argument must satisfy. It defaults to @code{t}. Look at the
947 @code{--clon-line-width} built-in option for an example.
949 @voindex{Enumerations,enum}@c
950 @cvopindex{:argument-name}@c
951 @vopindex{Enumerations,enum,:enum}@c
953 This option type is for options taking values from an enumerated set of
954 keywords. @code{enum} options change their default argument name from
955 @code{"ARG"} to @code{"TYPE"}. Also, they provide an additional property
956 called @code{:enum} to store the list of @cl{} keywords enumerating the
957 possible values. The end-user does not use a colon when providing an
958 argument to an @code{enum} option. Only the keyword's name. The end-user
959 also has the ability to abbreviate the possible values. An empty
960 argument is considered as an abbreviation for the first element in the
961 set. Look at the @code{--clon-version} built-in option for an example.
963 @bioindex{search-path}@c
965 @voindex{Paths,path}@c
966 @cvopindex{:argument-name}@c
967 @vopindex{Paths,path,:type}@c
968 This option type is for options taking a colon-separated list of
969 pathnames as argument. @code{path} options change their default argument
970 name from @code{"ARG"} to @code{"PATH"}. Also, they provide an
971 additional property called @code{:type} which specifies the kind of path
972 which is expected. Possible values are: @code{:file}, @code{:directory},
973 @code{:file-list}, @code{:directory-list} or @code{nil} (meaning that
974 anything is allowed). Null paths are allowed, and may be provided by an
975 empty argument. Look at the @code{--clon-search-path} and
976 @code{--clon-theme} built-in options for examples.
978 @voindex{Switches,switch}@c
979 @cvopindex{:argument-name}@c
980 @cvopindex{:argument-type}@c
981 @vopindex{Switches,switch,:argument-style}@c
982 This option type is for Boolean options. @code{switch} options change
983 their default argument type from required to optional and provide a
984 fallback value of @code{t} automatically for optional arguments.
985 @code{switch} options provide a new property called
986 @code{:argument-style}. Possible values are @code{:yes/no} (the
987 default), @code{:on/off}, @code{:true/false}, @code{:yup/nope},
988 @code{:yeah/nah}. This property affects the way the argument name and
989 true or false values are advertized in help strings. However, all
990 possible arguments forms (@pxenduserref{Switches}) are always available
993 @voindex{Extended Switches,xswitch}@c
994 @cvopindex{:argument-name}@c
995 @cvopindex{:argument-type}@c
996 @vopindex{Extended Switches,xswitch,:argument-style}@c
997 @vopindex{Extended Switches,xswitch,:enum}@c
998 @bioindex{highlight}@c
999 This option type stands for @dfn{extended} switch. Extended switches
1000 result from the mating of a male switch and a female enumeration, or the
1001 other way around (elementary decency prevents me from describing this
1002 mating process in detail): their possible values are either Boolean or
1003 from an @code{:enum} property as in the case of @code{enum} options. As
1004 simple switches, @code{xswitch} options change their default argument
1005 type from required to optional and provide a fallback value of @code{t}
1006 automatically for optional arguments. They also provide the
1007 @code{:argument-style} property. Contrary to switches, however, this
1008 property does not affect the argument name. It only affects the way true
1009 or false values are displayed in help strings. Look at the
1010 @code{--clon-highlight} built-in option for an example.
1013 @node Advanced Synopsis Creation, , Built-In Option Types, Synopsis Definition
1014 @subsection Advanced Synopsis Creation
1016 The fact that @code{defsynopsis} lets you define things in a
1017 @emph{declarative} way has not escaped you. Declarative is nice but
1018 sometimes it gets in the way, so it is time to see how things work under
1019 the hood. Every item in a synopsis is in fact implemented as an object
1020 (an instance of some class), so it turns out that @code{defsynopsis}
1021 simply is a convenience wrapper around the corresponding constructor
1022 functions for all such objects. Instead of using @code{defsynopsis}, you
1023 can then use those constructor functions explicitely.
1026 * Constructors:: The expansion of defsynopsis
1027 * Advantages:: Yes, but why?
1028 * Group Definition:: The declarative way
1031 @node Constructors, Advantages, Advanced Synopsis Creation, Advanced Synopsis Creation
1032 @subsubsection Constructors
1033 Let's have a look at the expansion of @code{defsynopsis} from the quick
1034 start example (@pxref{Quick Start}).
1036 The original code is like this:
1040 (defsynopsis (:postfix "FILES...")
1041 (text :contents "A very short program.")
1042 (group (:header "Immediate exit options:")
1043 (flag :short-name "h" :long-name "help"
1044 :description "Print this help and exit.")
1045 (flag :short-name "v" :long-name "version"
1046 :description "Print version number and exit.")))
1050 And once the macro is expanded, it will look like this:
1052 @cfindex make-synopsis
1056 @findex defsynopsis@r{, expansion}
1059 (make-synopsis :postfix "FILES..."
1060 :item (make-text :contents "A very short program.")
1061 :item (make-group :header "Immediate exit options:"
1062 :item (make-flag :short-name "h"
1064 :description "Print this help and exit.")
1065 :item (make-flag :short-name "v"
1066 :long-name "version"
1067 :description "Print version number and exit.")))
1071 As you can see, every synopsis element has a corresponding
1072 @code{make-@var{SOMETHING}} constructor, and the keywords used here and
1073 there in @code{defsyopsis} are in fact initargs to those constructors.
1074 We now examine those constructors in greater detail.
1076 @cindex Options, creation
1077 @defun make-text [@code{:hidden} BOOL] @code{:contents} STRING
1078 @cfsubindex{make-text}
1079 @cfiaindex{make-text,:contents}
1080 @cfiaindex{make-text,:hidden}
1081 Create an arbitrary text object, possibly hidden, whose contents is
1085 @c #### FIXME: I would like an environment without indexing here.
1086 @defun make-@var{OPTION} :INITARG INITVAL@dots{}
1087 @cfsubindex{make-@var{OPTION}}
1089 @cfindex make-stropt
1090 @cfindex make-lispobj
1093 @cfindex make-switch
1094 @cfindex make-xswitch
1095 Create a new @var{OPTION} object, @var{OPTION} being any built-in option
1096 type (@code{flag}, @code{stropt} @etc{}, see @ref{Built-In Valued
1097 Options}) or user-defined one (@pxref{New Option Types}). For a list of
1098 available initialization arguments (depending on the option type), see
1099 @ref{Built-In Option Types}.
1102 @defun make-group [@code{:header} STRING @code{:hidden} BOOL] @
1103 @code{:item} ITEM1 @code{:item} ITEM2@dots{}
1104 @cfsubindex{make-group}
1105 @cfiaindex{make-group,:header}
1106 @cfiaindex{make-group,:hidden}
1107 @cfiaindex{make-group,:item}
1109 Create a group object, possibly hidden, whose header is @var{STRING}.
1110 Every @var{ITEM} is an arbitrary text object, option object or group
1111 object. The order is important as it determines the display of the help
1115 @defun make-synopsis [@code{:postfix} STRING] @
1116 @code{:item} ITEM1 @code{:item} ITEM2@dots{}
1117 @cfsubindex{make-synopsis}
1118 @cfiaindex{make-synopsis,:postfix}
1119 @cfiaindex{make-synopsis,:item}
1121 Create a synopsis object whose postfix is @var{STRING}. Every @var{ITEM} is
1122 an arbitrary text object, option object or group object. The order is
1123 important as it determines the display of the help string.
1126 In fact, the @code{defsynopsis} macro allows you to freely mix
1127 declarative forms, constructor calls or whatever Lisp code you may want
1128 to use. The way this works is as follows: if a synopsis @var{ITEM}
1129 (@pxref{Synopsis Items}) is a list whose @code{car} is @code{text},
1130 @code{group}, or any option type (inluding those you define yourselves;
1131 see @ref{New Option Types}), then the @var{ITEM} is expanded as
1132 explained earlier. Otherwise, it is just left @emph{as-is}.
1134 To sum up, here's a example of things you can do in @code{defsynopsis}.
1138 (com.dvlsoft.clon:defsynopsis ()
1141 (setq *another-option* (com.dvlsoft.clon:make-switch #| ... |#)))
1146 @node Advantages, Group Definition, Constructors, Advanced Synopsis Creation
1147 @subsubsection Advantages
1149 So, why would you want to use constructors directly or mix declarative
1150 and imperative forms in @code{defsynopsis}? There are several reasons
1154 Some people prefer to declare (or should I say, create) their arbitrary
1155 texts, options and groups locally, in files, modules or ASDF components
1156 where they belong. In such a case, you need to keep references to the
1157 corresponding objects in order to compute the synopsis in the end.
1159 Since using constructors explicitely allows you to keep references to
1160 the created objects, these objects can be @emph{reused}. For instance,
1161 you can use the same text at different places, you can also use a single
1162 option several times, or even a single group several times so that its
1163 items appear in different places @etc{} Note that @clon{} knows its way
1164 around multiple occurrences of the same object: even if you use the same
1165 option object several times in a synopsis, @clon{} only maintains a
1166 single option definition.
1169 @node Group Definition, , Advantages, Advanced Synopsis Creation
1170 @subsubsection Group Definition
1171 There is one last point we need to address in order to complete this
1172 section. There might be times when you need to manipulate an explicit
1173 group object, but the object itself can still be created in a
1174 declarative (or mixed) way because you don't need to keep references on
1175 its items. For this, @clon{} provides a macro called @code{defgroup}.
1177 @defmac defgroup (OPTIONS@dots{}) ITEMS@dots{}
1178 @cindex Groups, in synopsis
1179 @fiindex{defsynopsis,group}
1180 @foindex{defgroup,:header}
1181 @foindex{defgroup,:hidden}
1182 Define a new group and return it. This macro behaves exactly like the
1183 @code{group} form in a call to @code{defsynopsis} (@pxref{Groups}). In
1184 fact, an intermediate step in the expansion of the @code{defsynopsis}
1185 macro is to transform @code{group} forms into @code{defgroup} macro
1186 calls. As for @code{defsynopsis}, @code{defgroup} allows you to mix
1187 declarative forms, constructor calls or any kind of Lisp code.
1195 @node Context Creation, Integrity Checks, Synopsis Definition, Using Clon
1196 @section Context Creation
1198 @cindex Initialization Phase
1199 @cindex Phase, initialization
1201 Step two of the @clon{} initialization phase consists in creating a
1202 @dfn{context}. A context is an object representing a particular instance
1203 of your program, for example (and most notably) with an actual
1204 @cmdline{} as the user typed it.
1207 * Making A Context:: The Constructor function
1208 * Contextual Information:: Interesting context slots
1211 @node Making A Context, Contextual Information, Context Creation, Context Creation
1212 @subsection Making A Context
1214 @cfindex{make-context}
1215 You create a context with the @code{make-context} function.
1218 @cfsubindex{make-context}
1219 Create a new context. That's it.
1222 @node Contextual Information, , Making A Context, Context Creation
1223 @subsection Contextual Information
1225 Once a context object is created, you have access to some interesting
1226 contextual information.
1229 * Program Name:: As it appear on the command-line
1230 * Command-Line Remainder:: The non-options part
1233 @node Program Name, Command-Line Remainder, Contextual Information, Contextual Information
1234 @subsubsection Program Name
1235 The application's program name, as it appears on the @cmdline{}, may be
1236 accessed from @clon{}. You may find it easier to do this way, as @clon{}
1237 wraps around implementation-dependent access methods to @code{argv[0]}.
1240 In order to retrieve @code{argv[0]}, use the @code{progname} function
1241 like this: @code{(progname)}.
1243 @node Command-Line Remainder, , Program Name, Contextual Information
1244 @subsubsection Command-Line Remainder
1245 @cindex Remainder, of command-line
1246 @cindex Command-Line, remainder
1247 In the case your @cmdline{} has a remainder (that is, a non-options
1248 part; see @enduserref{Option Separator} and @ref{Synopsis Items}), you
1249 may need to access it in order to process it independently from @clon{}.
1250 Since @clon{} is responsible for parsing the @cmdline{}, it is also in
1251 charge of figuring out where the remainder of the @cmdline{} begins.
1254 The @cmdline{} remainder is known to @clon{} as soon as a context is
1255 created. You can retrieve it by using the @code{remainder} function like
1256 this: @code{(remainder)}. The remainder is provided as a list of strings.
1263 @node Integrity Checks, Option Retrieval, Context Creation, Using Clon
1264 @section Integrity Checks
1266 @cfindex{make-context}
1267 At this point, you know about the two necessary steps to initialize
1268 @clon{}: defining a synopsis and creating a context. If you paid
1269 attention to the quick start application (@pxref{Quick Start}), you may
1270 have noticed that @code{defsynopsis} was called as a top-level form
1271 whereas @code{make-context} was called from the function @code{main}. So
1274 First, I hope that you see why a context cannot be created as a toplevel
1275 form. If you do that, you will end-up creating a context relevant to the
1276 Lisp environment from which the application is created, not run.
1278 The synopsis, on the other hand, could be defined either as a toplevel
1279 form, as done in the quick start and the demo programs, or in the
1280 function @code{main}, just before making a context. There is a very good
1281 reason to prefer a toplevel form however: that reason is called
1282 ``integrity checks''.
1284 When you define a synopsis (or any synopsis item, for that matter),
1285 @clon{} performs a number of checks to make sure that you're making a
1286 sensible use of the library. In fact, the number of semantic mistakes
1287 that you can make is quite puzzling. You could for instance define
1288 several options with identical names, forget to provide a fallback or
1289 default value when it is required, provide invalid fallback or default
1290 values, and the list goes on and on. These are just a few examples but
1291 there are many more, and @clon{} checks all of those (I think).
1293 Since those mistakes relate to the definition of the application itself,
1294 they do not depend on a particular execution of it. Consequently, the
1295 sooner @clon{} catches them, the better. If you define your
1296 application's synopsis as a toplevel form, @clon{} will be able to
1297 perform its integrity checks when the application is created, not only
1298 when it is used. In other words, you won't be able to get a working
1299 application until your use of @clon{} is semantically correct.
1301 This is why it is strongly recommended to create synopsis from toplevel
1302 forms, and this also explains why @clon{} chooses @emph{not} to provide
1303 an @code{initialize} function that would wrap around @code{defsynopsis}
1304 and @code{make-context} together.
1311 @node Option Retrieval, Help, Integrity Checks, Using Clon
1312 @section Option Retrieval
1313 @cindex Runtime Phase
1314 @cindex Phase, runtime
1315 @cindex Retrieval, of options
1316 @cindex Options Retrieval
1318 During the runtime phase of @clon{}, your main activity will be to
1319 retrieve options and their values. @clon{} provides two techniques for
1320 retrieving options: you can request the value for a specific option
1321 directly, or you can process the @cmdline{} sequentially, which is the
1322 more traditional approach.
1324 @cindex Command-Line
1326 Both of these techniques can be freely combined together at any time,
1327 because @clon{} keeps track of the current status of the @cmdline{}. In
1328 fact, @clon{} never works on the original @cmdline{}, but uses a mutable
1329 @emph{copy} of it after parsing. If you want to access the real
1330 @cmdline{} of your application, you may use the @code{cmdline} function,
1331 which is a wrapper around an implementation-dependent way to access it.
1334 Finally, remember that the @cmdline{} is scanned from left to right
1335 during option retrieval (@pxenduserref{Option Retrieval}).
1338 * Explicit Retrieval:: Accessing options by name
1339 * Sequential Retrieval:: Accessing options in order
1342 @node Explicit Retrieval, Sequential Retrieval, Option Retrieval, Option Retrieval
1343 @subsection Explicit Retrieval
1344 @cindex Options Retrieval, explicit
1345 @cindex Retrieval, of options, explicit
1346 Since @clon{} lets you retrieve options on demand (at any time), it
1347 makes sense to be able to request the value of a specific option
1348 explicitely. For instance, you might want to try the @samp{--help}
1349 option first, without looking at the rest of the @cmdline{} because the
1350 application will in fact quit immediately after having displayed the
1353 @defun getopt :KEY VALUE@dots{}
1354 Retrieve the value of a specific option. The following @var{:KEY}s are
1355 currently available.
1360 @fkindex{getopt,:short-name}
1361 @fkindex{getopt,:long-name}
1362 Use one of these 2 keys to specify the name of the option you wish to
1365 @fkindex{getopt,:option}
1366 Alternatively, you can use a reference to an option object
1367 (@pxref{Constructors}).
1370 This function return two values: the option's value and the value's
1371 source (@pxenduserref{Value Sources}).
1374 The value's source may have the following forms:
1376 @item (:cmdline @var{NAME})
1377 @cindex Values, source, @cmdline{}
1378 This is for options found on the @cmdline{}. @var{NAME} is the name used
1379 on the @cmdline{}. It can be the option's long or short name, or a
1380 completed long name if the option's name was abbreviated. A completed
1381 name displays the omitted parts in parentheses (@code{"he(lp)"} for
1383 @item (:fallback @var{NAME})
1384 @cvopindex{:fallback-value}
1385 @cindex Values, source, fallback
1386 The same but when the fallback value is used, that is, when an option is
1387 not provided with its (optional) argument.
1388 @item (:default @var{NAME})
1389 @cvopindex{:default-value}
1390 @cindex Values, source, default
1391 The same, but when the default value is used (because there is no
1393 @item (:environment @var{VAR})
1395 @cindex Values, source, environment
1396 This is for options not found on the @cmdline{} but for which there is
1397 an associated environment variable set in the application's environment.
1398 @var{VAR} is the name of the corresponding environment variable.
1400 @cvopindex{:default-value}
1401 @cindex Values, source, default
1402 This is for options not found anywhere, but for which a default value
1408 Note that because flags don't take any argument, @code{getopt} returns a
1409 virtual value of @code{t} when they are found or a corresponding
1410 environment variable exists in the environment. For the same reason, a
1411 flag's value source may ony be @code{(:cmdline @var{NAME})} or
1412 @code{(:environment @var{VAR})}.
1415 When an option is not found anywhere and there is no default value,
1416 @code{getopt} just returns nil (no second value). Also, note that when
1417 your option accepts @code{nil} as a value, you @emph{need} to handle the
1418 second retrun value to make the difference between an option not found,
1419 and an actual value of @code{nil}.
1422 @node Sequential Retrieval, , Explicit Retrieval, Option Retrieval
1423 @subsection Sequential Retrieval
1424 @cindex Options Retrieval, sequential
1425 @cindex Retrieval, of options, sequential
1426 The more traditional approach to option retrieval is to scan the
1427 @cmdline{} for options in their order of appearance. @clon{} supports
1428 this by providing you with one function and two macros, as explained
1431 @defun getopt-cmdline
1432 Get the @dfn{next} @cmdline{} option, that is, the first option on the
1433 @cmdline{} that has not been previously retrieved, either explicitely or
1436 When there are no more options on the @cmdline{}, this function returns
1437 @code{nil}. Otherwise, four values are returned: the corresponding
1438 option object from the synopsis definition (@pxref{Constructors}), the
1439 name used on the @cmdline{}, the option's value and the value source
1440 (@code{:cmdline}, @code{:fallback} or @code{:default}). As in the case
1441 of explicit retrieval (@pxref{Explicit Retrieval}), the option's name
1442 may be completed in case of abbreviation.
1444 @rfindex{short-name}
1446 @copindex{:short-name}
1447 @copindex{:long-name}
1448 Unless you keep references to all your option objects (and thus can
1449 compare them directly to the one returned by this function), you can
1450 still identify the retrieved option by using the @code{short-name} and
1451 @code{long-name} readers on it: simply use @code{(long-name
1452 @var{OPTION})} or @code{(short-name @var{OPTION})} and you will get the
1453 corresponding strings.
1456 @defmac multiple-value-getopt-cmdline (OPTION NAME VALUE SOURCE) BODY
1457 Evaluate @var{BODY} with @var{OPTION}, @var{NAME}, @var{VALUE} and
1458 @var{SOURCE} bound to the values returned by the @code{getopt-cmdline}
1459 function above. Note that @var{BODY} is not evaluated if there was no
1460 remaining option on the @cmdline{}, so you don't need to conditionalize
1461 on @var{OPTION} being @code{null} yourself.
1464 @defmac do-cmdline-options (OPTION NAME VALUE SOURCE) BODY
1465 As above, but loop over all @cmdline{} options.
1472 @node Help, , Option Retrieval, Using Clon
1475 One of the first motivations in the design of @clon{} was to automate
1476 the generation of the help string, which is a very boring maintenance
1477 task to do by hand. The application's synopsis contains all the
1478 necessary information to do so. In order to print your application's
1479 help string, use the @code{help} function.
1481 @defun help [@code{:item} ITEM]
1482 @bioindex{search-path}
1483 @evindex{CLON_SEARCH_PATH}
1484 @bioindex{highlight}
1485 @evindex{CLON_HIGHLIGHT}
1487 @evindex{CLON_THEME}
1488 @bioindex{line-width}
1489 @evindex{CLON_LINE_WIDTH}
1490 Print the application's help string. Printing honors the search path,
1491 theme, line width and highlight settings provided by the corresponding
1492 built-in options (@pxenduserref{Theme Mechanism}).
1495 @fkindex{help,:item}
1496 By default, @code{help} prints the whole application help string,
1497 excluding hidden items. However, if you have kept a reference to any
1498 synopsis item (option, text, group), you can pass it as the value of the
1499 @code{:item} key, and @clon{} will only print the help string for that
1500 particular item. In this case however, the hidden state of the item is
1501 discarded (but @emph{not} the ones of sub-items).
1504 @cindex Built-In Groups
1505 @cindex Groups, built-in
1506 @cindex Hidden Groups
1507 @cindex Groups, hidden
1508 For instance, the @clon{} built-in group is normally hidden, so it
1509 doesn't show up in the global help string, but the @option{--clon-help}
1510 option uses the @code{help} function on it explicitely, so it discards
1513 Here is a potentially useful application of hidden groups in conjunction
1514 with the @code{:item} key. Look at ImageMagick's @file{convert}
1515 program's help string for instance: it is 276 lines long. Gosh. The help
1516 string is decomposed into several categories: image settings, image
1517 operators, misc options @etc{}. If I were to implement this program, I
1518 would rather have the @option{--help} option display an overview of the
1519 program, advertise @option{--version} and a couple of others, and I
1520 would then implement @option{--help} as an enumeration for listing every
1521 option category individually (they would normally be stored in hidden
1522 groups). The user could then use @option{--help=settings},
1523 @option{--help=operators} and so on to display only the category she's
1526 Finally, here is potentially useful application of hidden options that
1527 are never ever displayed in any help string whatsoever, and I mean,
1528 like, ever. This is the perfect tool for backdoor'ing a program. For
1529 instance, if you ever need to implement a
1530 @option{--discard-all-security-measures-and-blow-the-nuke} option, then
1531 you'd better have it hidden@dots{}
1535 @c ====================================================================
1537 @c ====================================================================
1538 @node Extending Clon, Advanced Usage, Using Clon, Top
1539 @chapter Extending Clon
1541 As you know, @clon{} already provides seven built-in option types: flags
1542 and six other types for valued options (@pxref{Built-In Valued
1543 Options}). After using @clon{} for a while, you may find that however
1544 brilliant and perfectly designed it is, none of the provided built-in
1545 types fullfill your requirements exactly. There are two ways around
1546 this: the right way and the wrong way (hint).
1548 @voindex{Strings,stropt}
1549 The wrong, although perhaps quicker way would be to use the
1550 @code{stropt} option type to simply retrieve unprocessed string values,
1551 and then do whatever tweaking required on them. In doing so, you risk
1552 reinventing some of @clon{}'s wheels.
1554 The right way is to define a new option type. Properly defined option
1555 types are a good thing because they allow for reusability and also
1556 extensibility, since new option types can always be defined on top of
1558 @voindex{Enumerations,enum}
1559 In this chapter we explain how to extend @clon{} by providing new option
1560 types. We illustrate the process with the example of the built-in
1563 @cindex Valued Options
1564 @cindex Options, valued
1566 Oh, and I almost forgot. I hope it is obvious to everyone that new
1567 option types are always @emph{valued}. There's no point in extending
1568 @clon{} with options that don't take any arguments, since we already
1572 * New Option Types:: In four steps
1573 * Extension Tips:: Some advice
1580 @node New Option Types, Extension Tips, Extending Clon, Extending Clon
1581 @section New Option Types
1582 @cindex Option Types, in files
1583 @cindex Files, one per option type
1584 @pkgindex{com.dvlsoft.clon}
1585 From a software engineering point of view, it is better to implement new
1586 option types in a file of their own, preferably named after the option
1587 type itself, and to put this file in the @clon{} package, like this:
1591 (in-package :com.dvlsoft.clon)
1595 Creating your own option type involves 5 steps: providing a class for
1596 them, implementing three protocols related to argument/value tweaking,
1597 and providing a constructor function. We now review those 5 steps in
1601 * New Option Classes:: Step 1
1602 * Value Check Protocol:: Step 2
1603 * Argument Conversion Protocol:: Step 3
1604 * Error Management:: In-between steps coffee time!
1605 * Value Stringification Protocol:: Step 4
1606 * Constructor Functions:: Step 5
1609 @node New Option Classes, Value Check Protocol, New Option Types, New Option Types
1610 @subsection New Option Classes
1612 @clsindex{valued-option}
1613 @clon{} maintains a class hierarchy for all option types. The mother of
1614 all option types is the @code{option} abstract class. It handles the
1615 options'short and long names, description and associated environment
1616 variable (@pxref{Common Properties}). Valued options inherit from an
1617 abstract subclass of @code{option} called @code{valued-option}. This
1618 class handles the option's argument name and status (optional or
1619 mandatory), fallback and default values (@pxref{Common Valued Option
1622 @cindex User-Defined Option Classes
1623 @cindex Option Classes, user-defined
1624 In order to create a new option type, use the @code{defoption} macro.
1626 @defmac defoption CLASS SUPERCLASSES SLOTS &rest OPTIONS
1627 Create a new option CLASS and register it with @clon{}.
1628 Syntactically, thismacro behaves like @code{defclass}. Option types
1629 created like this implicitely inherit from @code{valued-option} and in
1630 turn @code{option}, so you don't need to put them explicitely in the
1631 @var{SUPERCLASSES} list.
1634 Let's look at the enumeration example now.
1638 (defoption enum (enum-base)
1639 ((argument-name ;; inherited from the VALUED-OPTION class
1641 (:documentation "The ENUM class.
1642 This class implements options whose values belong to a set of keywords."))
1646 @voindex{Enumerations,enum}
1647 @vopindex{Enumerations,enum,:enum}
1648 @voindex{Extended Switches,xswitch}
1649 @vopindex{Extended Switches,xswitch,:enum}
1650 @clsindex{enum-base}
1651 As you can see, this class inherits from @code{enum-base}, which is the
1652 class handling the @code{:enum} property. The reason for this split is
1653 that there are currently two option types providing enumeration-like
1654 facility: @code{enum} and @code{xswitch}, so @code{xswitch} also
1655 inherits from @code{enum-base}.
1657 @clsindex{valued-option}
1658 @cvopindex{:argument-name}
1659 There are no new slots in this class, but the @code{argument-name} slot
1660 provided by the @code{valued-option} class has its initform changed from
1661 @code{"ARG"} to @code{"TYPE"}.
1663 @node Value Check Protocol, Argument Conversion Protocol, New Option Classes, New Option Types
1664 @subsection Value Check Protocol
1665 @cindex Value Check Protocol
1666 @cindex Protocols, value check
1667 Now that we have our new option class, we need to implement the
1668 so-called @dfn{value check} protocol. This protocol is used to make sure
1669 that values provided for options of your new type actually comply with
1670 the type in question. Values going through this protocol are fallback
1671 values, default values, and values provided from a debugger restart
1672 (@pxenduserref{Error Management}). In the case of fallback and default
1673 values (which, by the way, are provided by @emph{you}, the @clon{}
1674 user), the check is performed only once, when the option object is
1675 created. Values provided from a debugger restart come from the
1676 application end-user, and hence are checked every time.
1678 The value check protocol is implemented through a @code{check} generic
1679 function for which you must provide a method.
1681 @deffn {Generic Function} check OPTION VALUE
1683 @ecindex invalid-value
1684 Check that @var{VALUE} is valid for @var{OPTION}. If @var{VALUE} is
1685 valid, return it. Otherwise, raise an @code{invalid-value} error.
1688 As you can see, you need to provide a method with the first argument
1689 specialized to your new option type. This method must return @var{VALUE}
1690 if it is okay, and raise an @code{invalid-value} error otherwise.
1692 @ecindex invalid-value
1693 @clon{} maintains a hierarchy of error conditions. The
1694 @code{invalid-value} error condition is defined like this:
1698 (define-condition invalid-value (option-error)
1699 ((value :documentation "The invalid value."
1702 (comment :documentation "An additional comment about the error."
1706 (:report (lambda (error stream)
1707 (format stream "Option ~A: invalid value ~S.~@[~%~A~]"
1708 (option error) (value error) (comment error))))
1709 (:documentation "An invalid value error."))
1713 @ecindex invalid-value
1714 @ecsindex{invalid-value,value}
1715 @ecsindex{invalid-value,comment}
1716 When the error is raised, you must fill in the @code{value} and
1717 @code{comment} slots appropriately.
1718 @ecindex option-error
1719 @ecsindex{option-error,option}
1720 The super-condition @code{option-error} provides an additional
1721 @code{option} slot that you must also fill in when the error is raised.
1723 @gfmindex{check,@r{Enumerations (}enum@r{)}}
1724 @voindex{Enumerations,enum}
1725 @vopindex{Enumerations,enum,:enum}
1726 @findex list-to-string
1727 Let's look at the enumeration example now.
1731 (defmethod check ((enum enum) value)
1732 "Check that VALUE is a valid ENUM."
1733 (unless (member value (enum enum))
1734 (error 'invalid-value
1737 :comment (format nil "Valid values are: ~A."
1738 (list-to-string (enum enum)
1739 :key #'prin1-to-string))))
1744 This code should be self-explanatory. We check that the value we got
1745 belongs to the enumeration. @code{list-to-string} is a utility function
1746 that will separate every element with comas in the resulting string.
1748 @node Argument Conversion Protocol, Error Management, Value Check Protocol, New Option Types
1749 @subsection Argument Conversion Protocol
1750 @cindex Argument Conversion Protocol
1751 @cindex Protocols, argument conversion
1752 The next protocol we need to implement is the so-called @dfn{argument
1753 conversion} protocol. This protocol is used to convert option arguments
1754 (that is, strings) to an actual value of the proper type. Arguments
1755 going through this protocol come from the @cmdline{}, the value of an
1756 environment variable or a debugger restart (@pxenduserref{Error
1757 Management}). Also, note that @clon{} assumes that you implement this
1758 protocol correctly, so no value check is performed on values coming from
1759 the conversion of an argument.
1761 The conversion protocol is implemented through a @code{convert} generic
1762 function for which you must provide a method.
1764 @deffn {Generic Function} convert OPTION ARGUMENT
1765 @gfsubindex{convert}
1766 @ecindex invalid-argument
1767 Convert @var{ARGUMENT} to @var{OPTION's} value. If @var{ARGUMENT} is
1768 invalid, raise an @code{invalid-argument} error.
1771 As you can see, you need to provide a method with the first argument
1772 specialized to your new option type. This method must return the
1773 conversion of @var{ARGUMENT} to the appropriate type if it is valid, and
1774 raise an @code{invalid-argument} error otherwise.
1776 @ecindex invalid-argument
1777 The @code{invalid-argument} error condition is defined like this:
1781 (define-condition invalid-argument (option-error)
1782 ((argument :documentation "The invalid argument."
1786 (comment :documentation "An additional comment about the error."
1790 (:report (lambda (error stream)
1791 (format stream "Option ~A: invalid argument ~S.~@[~%~A~]"
1792 (option error) (argument error) (comment error))))
1793 (:documentation "An invalid argument error."))
1797 @ecindex invalid-argument
1798 @ecsindex{invalid-argument,argument}
1799 @ecsindex{invalid-argument,comment}
1800 When the error is raised, you must fill in the @code{argument} and
1801 @code{comment} slots appropriately.
1802 @ecindex option-error
1803 @ecsindex{option-error,option}
1804 As before, the super-condition @code{option-error} provides an
1805 additional @code{option} slot that you must also fill in when the error
1808 @gfmindex{convert,@r{Enumerations (}enum@r{)}}
1809 @voindex{Enumerations,enum}
1810 @vopindex{Enumerations,enum,:enum}
1811 @findex closest-match
1812 Let's look at the enumeration example now.
1816 (defmethod convert ((enum enum) argument)
1817 "Convert ARGUMENT to an ENUM value."
1818 (or (closest-match argument (enum enum) :ignore-case t :key #'symbol-name)
1819 (error 'invalid-argument
1822 :comment (format nil "Valid arguments are: ~A."
1823 (list-to-string (enum enum)
1824 :key (lambda (value)
1825 (stringify enum value)))))))
1830 Since enumerations allow their arguments to be abbreviated, a utility
1831 function named @code{closest-match} is used to find the closest match
1832 between an argument and the possible values. Otherwise, an
1833 @code{invalid-argument} error is raised. For an explanation of
1834 @code{stringify}, @xref{Value Stringification Protocol}.
1836 @node Error Management, Value Stringification Protocol, Argument Conversion Protocol, New Option Types
1837 @subsection Error Management
1839 @cindex Debugger restarts
1840 Let's take a short break in our @clon{} extension process. We have seen
1841 that @clon{} may throw errors in different situations, including invalid
1842 arguments or values. The end-user manual advertises a set of debugger
1843 restarts that are available to fix those kinds of problems interactively
1844 (if you don't know what I'm talking about, please read @enduserref{Error
1845 Management} and you will know). For that reason, you are encouraged to
1846 @emph{not} disable the debugger in your standalone program, at least
1847 when you're executing @clon{} code.
1849 @node Value Stringification Protocol, Constructor Functions, Error Management, New Option Types
1850 @subsection Value Stringification Protocol
1851 Okay, back to implementing our new option type.
1853 @cindex Value Stringification Protocol
1854 @cindex Protocols, value stringification
1855 The third and last protocol we need to implement is called the
1856 @dfn{value stringification} protocol. This protocol can be seen as the
1857 reverse protocol for argument conversion (@pxref{Argument Conversion
1858 Protocol}): its purpose is to transform an option's value into a
1859 corresponding argument that the end-user could have provided in order to
1863 @cvopindex{:fallback-value}
1864 @cindex Values, source, fallback
1865 @cvopindex{:default-value}
1866 @cindex Values, source, default
1867 @gfmindex{convert,@r{Enumerations (}enum@r{)}}
1868 The main use for this protocol is to advertise the fallback and default
1869 values correctly in help strings: the end-user does not want to see
1870 those @emph{values}, but rather the @emph{argument} that would lead to
1871 them. However, you are free to use it wherever you like (see the
1872 @code{convert} method for @code{enum} options for instance).
1874 The value stringification protocol is implemented through a
1875 @code{stringify} generic function for which you must provide a method.
1877 @deffn {Generic Function} stringify OPTION VALUE
1878 @gfsubindex{stringify}
1879 Transform OPTION's @var{VALUE} into an argument.
1882 I admit that this function could also have been called
1883 @code{argumentize} or even @code{deconvertify}. As you can see, you need
1884 to provide a method with the first argument specialized to your new
1885 option type. You can assume that @var{VALUE} is a valid value for your
1886 option, so no checking is necessary and no error needs to be raised.
1888 @gfmindex{stringify,@r{Enumerations (}enum@r{)}}
1889 @voindex{Enumerations,enum}
1890 Let's look at the enumeration example now.
1894 (defmethod stringify ((enum enum) value)
1895 "Transform ENUM's VALUE into an argument."
1896 (string-downcase (symbol-name value)))
1900 Pretty straightforward, right?
1903 @node Constructor Functions, , Value Stringification Protocol, New Option Types
1904 @subsection Constructor Functions
1905 @cindex Constructors, for options objects
1906 @cindex Option Constructors
1907 The last required step to complete our new option type extension is to
1908 provide a @dfn{constructor} function that wraps around
1909 @code{make-instance} on the corresponding option class. I won't insult
1910 you by explaining how to write a constructor. Let me just give four good
1911 reasons why providing constructors is important.
1913 Providing a constructor for every new option type is important because:
1918 it is a good software engineering practice,
1924 and above all, it makes your new option type automatically available in
1925 calls to @code{defsynopsis} and @code{defgroup} (@pxref{Synopsis Items}
1926 and @ref{Group Definition}).
1930 @voindex{Enumerations,enum}
1932 Let's look at the enumeration example now.
1936 (defun make-enum (&rest keys
1937 &key short-name long-name description
1938 argument-name argument-type
1939 enum env-var fallback-value default-value
1941 "Make a new enum option.
1942 - SHORT-NAME is the option's short name (without the dash).
1944 - LONG-NAME is the option's long name (without the double-dash).
1946 - DESCRIPTION is the option's description appearing in help strings.
1948 - ARGUMENT-NAME is the option's argument name appearing in help strings.
1949 - ARGUMENT-TYPE is one of :required, :mandatory or :optional (:required and
1950 :mandatory are synonyms).
1951 It defaults to :optional.
1952 - ENUM is the set of possible values.
1953 - ENV-VAR is the option's associated environment variable.
1955 - FALLBACK-VALUE is the option's fallback value (for missing optional
1957 - DEFAULT-VALUE is the option's default value, if any.
1958 - When HIDDEN, the option doesn't appear in help strings."
1959 (declare (ignore short-name long-name description
1960 argument-name argument-type
1961 enum env-var fallback-value default-value
1963 (apply #'make-instance 'enum keys))
1967 Woah, what a mouthful for a single line of code@dots{} Yeah, I'm a
1968 maniac and I like redundancy. I always restate all the available keys
1969 explicitely, and everything again in the docstring so that all the
1970 interesting information is directly available (I might change my mind as
1971 I grow older though).
1977 @node Extension Tips, , New Option Types, Extending Clon
1978 @section Extension Tips
1979 So that's it. Now you know how to extend @clon{} with your own option
1980 types. Here is some piece of advice that you might find useful in the
1984 * Incremental Option Types:: Doing without defoption
1985 * Lisp Option Abuse:: Thinking in end-user terms
1988 @node Incremental Option Types, Lisp Option Abuse, Extension Tips, Extension Tips
1989 @subsection Incremental Option Types
1991 If one of the built-in options is almost what you need, you may be
1992 tempted to subclass it directly instead of using @code{defoption}, and
1993 only change what's needed, After all, it's Lisp. Lisp is a world of
1994 mess@t{^D^D^D^D}freedom.
2000 @code{defoption} is not @emph{only} a convenience wrapper around
2001 @code{defclass}. It also arranges for @code{defsynopsis} and
2002 @code{defgroup} to recognize your new option type. So please, do use it
2005 @node Lisp Option Abuse, , Incremental Option Types, Extension Tips
2006 @subsection Lisp Option Abuse
2007 @voindex{Lisp Objects,lispobj}
2008 @vopindex{Lisp Objects,lispobj,:typespec}
2009 Along with the same lines, you may find that the @code{lispobj} type is
2010 all you need in many situations. Let's take an example. Suppose you want
2011 to implement a @code{--stars} option to assign a rank to a movie, from 0
2012 to 5. The lazy approach is to simply create a @code{lispobj} option with
2013 a @code{:typespec} (type specifier) of @code{(integer 0 5)} and you're
2016 But now, remember that the end-user of your application is probably not
2017 a Lisper (in fact, I would hope that @clon{} contributes to increasing
2018 the number of standalone @cl{} applications out there@dots{}). What do
2019 you think would be her reaction, if, after providing a bogus value to
2020 the @code{--stars} option, she get the following error message:
2023 Option 'stars': invalid argument "6".
2024 Argument "6" must evaluate to (integer 0 5).
2027 or worse, a @code{"Cannot parse argument"} error message because of a
2030 Not very friendly, right? In other words, you need to think in terms of
2031 what the end-user of your application will expect. In that particular
2032 situation, you might want to subclass @code{lispobj} (with
2033 @code{defoption}!) only to provide friendlier error messages.
2037 @c ====================================================================
2039 @c ====================================================================
2040 @node Advanced Usage, Conclusion, Extending Clon, Top
2041 @chapter Advanced Usage
2043 This chapter contains information about different features that are
2044 present in @clon{} because of design decisions, but that I expect to be
2045 used only rarely, if at all.
2048 * Multiple Clon Instances:: Different command-lines, synopsis or contexts
2049 * Programmatic Help Strings:: Beyond the --help option
2050 * Version Numbering:: In case you need to conditionalize
2054 @c -----------------------
2055 @c Multiple Clon Instances
2056 @c -----------------------
2057 @node Multiple Clon Instances, Programmatic Help Strings, Advanced Usage, Advanced Usage
2058 @section Multiple @clon{} Instances
2060 It is possible to use different instances of @clon{} in parallel in a
2061 single application, by using a virtual @cmdline{} instead of the real
2062 one, different synopsis and multiple contexts simultaneously.
2065 * Using Different Synopsis:: The default one, and the others
2066 * Using Different Command-Lines:: The real one, and the others
2067 * Using Multiple Contexts:: The current one, and the others
2068 * Potential Uses:: What to do with all that
2071 @node Using Different Synopsis, Using Different Command-Lines, Multiple Clon Instances, Multiple Clon Instances
2072 @subsection Using Different Synopsis
2073 @cindex Synopsis, default
2074 Did you notice that after defining a synopsis, there is actually never
2075 any explicit reference to it anymore? So where is the magick? In fact,
2076 there's no magick at all involved here.
2078 @vindex *default-synopsis*
2079 @foindex{defsynopsis,:make-default}
2080 @cfiaindex{make-synopsis,:make-default}
2081 @clon{} has a global variable named @code{*default-synopsis*} which
2082 holds the@dots{} default synopsis, yeah. When you define/create a
2083 synopsis with either @code{defsynopsis} or @code{make-synopsis}, it is
2084 automatically made the default one, unless you use the
2085 @code{:make-default} option/initarg with a value of @code{nil}, like
2089 (defsynopsis (:make-default nil) ...)
2095 (make-synopsis :make-default nil ...)
2098 @cfindex{make-context}
2099 When you create a context with @code{make-context}, the default synopsis
2100 is used implicitely, but you have two ways to avoid this.
2104 @vindex *default-synopsis*
2105 At any time in your program, you may change the value of
2106 @code{*default-synopsis*}. All subsequent calls to @code{make-context}
2107 will hence use this other synopsis.
2109 @cfiaindex{make-context,:synopsis}@c
2110 If you prefer to use another synopsis only temporarily, you can use the
2111 @code{:synopsis} initarg to @code{make-context} instead.
2114 @node Using Different Command-Lines, Using Multiple Contexts, Using Different Synopsis, Multiple Clon Instances
2115 @subsection Using Different Command-Lines
2116 In @ref{Context Creation}, we saw that a context object describes a
2117 particular instance of your application, most notably depending on the
2118 actual @cmdline{} the end-user provided. It turns out, however that the
2119 @cmdline{} doesn't need to be the actual program's @cmdline{}, as the
2120 user typed it. Any list of strings can act as a @cmdline{}.
2122 @cfiaindex{make-context,:cmdline}
2123 The function @code{make-context} has a @code{:cmdline} key that allows
2124 you to provide any list of strings that will act as the @cmdline{}. Of
2125 course, the default is to use the actual program's one.
2127 @node Using Multiple Contexts, Potential Uses, Using Different Command-Lines, Multiple Clon Instances
2128 @subsection Using Multiple Contexts
2129 @cindex Context, current
2130 Did you also notice that after creating a context, there is actually
2131 never any explicit reference to it anymore? So again, where is the
2132 magick? In fact, there's no magick at all involved here either.
2134 @vindex *current-context*
2135 @cfiaindex{make-context,:make-current}
2136 @clon{} has a global variable named @code{*current-context*} which holds
2137 the@dots{} current context, yeah. When you create a context with
2138 @code{make-context}, it is automatically made current, unless you use
2139 the @code{:make-current} initarg with a value of @code{nil}.
2141 @cindex Runtime Phase
2145 @findex getopt-cmdline
2146 @findex multiple-value-getopt-cmdline
2147 @findex do-cmdline-options
2149 The whole runtime phase API of @clon{} uses a context implicitely. This
2150 involves @code{progname}, @code{remainder}, @code{getopt},
2151 @code{getopt-cmdline}, @code{multiple-value-getopt-cmdline},
2152 @code{do-cmdline-options} and @code{help}. As a consequence, it is
2153 possible to use @clon{} with multiple contexts at the same time. There
2154 are in fact three ways to achieve this.
2158 @vindex *current-context*
2159 At any time in your program, you may change the value of
2160 @code{*current-context*}. All subsequent calls to the runtime phase API
2161 will hence use this other context.
2163 @clon{} also provides a macro which changes the current context for you.
2164 @defun with-context CONTEXT &body BODY
2165 Execute @var{BODY} with @code{*current-context*} bound to @var{CONTEXT}.
2168 @fkindex{progname,:context}@c
2169 @fkindex{remainder,:context}@c
2170 @fkindex{getopt,:context}@c
2171 @fkindex{getopt-cmdline,:context}@c
2172 @fkindex{help,:context}@c
2173 @foindex{multiple-value-getopt-cmdline,:context}@c
2174 @foindex{do-cmdline-options,:context}@c
2175 If you prefer to use another context only once, you can use the
2176 @code{:context} key instead. The whole runtime phase API of @clon{}
2177 understands it. For the functions @code{getopt}, @code{getopt-cmdline}
2178 and @code{help}, it's just another key in addition to those we've
2179 already seen. For the macros @code{multiple-value-getopt-cmdline} and
2180 @code{do-cmdline-options}, the key must appear at the end of the first
2181 (list) argument, like this:
2184 (multiple-value-getopt-cmdline (option name value :context ctx) ...)
2185 (do-cmdline-options (option name value :context ctx) ...)
2189 @node Potential Uses, , Using Multiple Contexts, Multiple Clon Instances
2190 @subsection Potential Uses
2191 By combining @clon{}'s ability to use a virtual @cmdline{}, different
2192 synopsis and multiple contexts, you can achieve very neat (read: totally
2193 useless) things. For instance, you could write an application that takes
2194 an option providing @cmdline{} arguments for an external program to be
2195 forked. Some revision control systems do that for controlling external
2196 @code{diff} programs for instance, so no big deal. The big deal is that
2197 you can completely control the validity of the external program's
2198 @cmdline{}, before it is forked, from your original one.
2200 Here is another idea, again related to revision control systems. Some of
2201 them feature a @cmdline{} syntax like the following:
2204 prog [global options] command [command-specific options]
2208 @cindex Command-Line, remainder
2209 @cindex Remainder, of command-line
2210 You can achieve this with @clon{} quite easily. In fact, the
2211 demonstration program called @file{advanced} in the distribution shows
2212 you how to do it. First, define a synopsis which only handles the global
2213 options, and provide a postfix of @code{"command [command-specific
2214 option]"} or something like that. This will authorize a @cmdline{}
2215 remainder which will start with the command name.
2217 Now, for every command in your program, define a specific synopsis with
2218 only the command-specific options. Get the remainder of the original
2219 @cmdline{} (@pxref{Command-Line Remainder}) and figure out which command
2220 was used. Depending on it, create a new context with the appropriate
2221 synopsis and the original @cmdline{}'s remainder as the new, virtual,
2222 @cmdline{}. You're done: retrieve global options from the first context,
2223 and command-specific ones from the second one.
2225 @cindex Help String, display
2227 What's even cooler is that you can display the command-specific options
2228 on demand quite easily as well (like what @file{git} does when you call
2229 it like this: @code{git commit --help} for instance): calling the
2230 @code{help} function on the original context gives you the global
2231 options's help string while calling it on the command-specific one will
2232 display the command-specific usage.
2234 @bioindex{search-path}
2235 @evindex{CLON_SEARCH_PATH}
2236 @bioindex{highlight}
2237 @evindex{CLON_HIGHLIGHT}
2239 @evindex{CLON_THEME}
2240 @bioindex{line-width}
2241 @evindex{CLON_LINE_WIDTH}
2242 One thing to remember here is that every context/synopsis duet you
2243 create gets its own set of built-in @clon{} options. As a consequence,
2244 there is currently no simple way to have a single set of built-in
2245 options apply to the whole application, for instance, to both a global
2246 and a command-specific context. Let me make this clearer: if your
2247 end-user calls @code{prog --clon-theme=foo command -h}, then the theme
2248 option will have no effect because it would only affect the global help
2249 option. In order to actually use the expected theme, your end-user would
2250 need to use @code{prog command --clon-theme=foo -h}. Depending on which
2251 cerebral emisphere (s)he prefers to use, this may seem logical or not.
2253 Finally, note that you can use the virtual @cmdline{} / specific
2254 synopsis technique recursively to manage complicated @cmdline{} syntax,
2255 for instance alternating options and non-options parts several times.
2257 In the future, @clon{} may provide better ways to achieve this kind of
2258 things (a notion of ``sub-context'' may be in order).
2262 @c -------------------------
2263 @c Programmatic Help Strings
2264 @c -------------------------
2265 @node Programmatic Help Strings, Version Numbering, Multiple Clon Instances, Advanced Usage
2266 @section Programmatic Help Strings
2271 @cindex Themes, standard, @t{refcard}
2272 @cindex Standard Themes, @t{refcard}
2273 So far, we've seen how to use the @code{help} function to implement a
2274 typical @code{--help} option. This is mostly intended for the end-user.
2275 There are also times when this function could be useful to @emph{you},
2276 the application developer. For instance, one could imagine that part of
2277 the compilation phase would involve generating the help string in order
2278 to include it in the manual. Another idea would be that @samp{make
2279 install} creates a @file{REFCARD} file in
2280 @file{/usr/local/share/doc/my-app/} which contains the help string
2281 formatted with the @code{refcard} theme, @etc{}.
2284 @evindex{CLON_THEME}
2285 @bioindex{search-path}
2286 @evindex{CLON_SEARCH_PATH}
2287 @bioindex{line-width}
2288 @evindex{CLON_LINE_WIDTH}
2289 @bioindex{highlight}
2290 @evindex{CLON_HIGHLIGHT}
2291 In such situations, calling the @code{help} function might not be
2292 directly associated with an end-user level option, or at least not
2293 @code{--help}, and you might not want to honor the end-user level
2294 settings for theme, search path, line-width, or highlighting either
2295 (remember that these settings might come from the environment variables
2296 associated with @option{--clon-theme}, @option{--clon-search-path},
2297 @option{--clon-line-width} and @option{--clon-highlight}).
2299 @fkindex{help,:theme}
2300 @fkindex{help,:search-path}
2301 @fkindex{help,:line-width}
2302 @fkindex{help,:highlight}
2303 Because of this, the @code{help} function provides additional keys that
2304 allow you to override those settings (they are in fact stored in the
2305 context object). The keys in question are: @code{:theme},
2306 @code{:search-path}, @code{:line-width} and @code{:highlight}.
2308 @fkindex{help,:output-stream}
2309 @vindex *standard-output*
2310 In addition to that, there is an @code{:output-stream} key which
2311 defaults to @code{*standard-output*} which you could use for instance to
2312 write directly to a file. Note that there is no end-user level access to
2317 @c -----------------
2318 @c Version Numbering
2319 @c -----------------
2320 @node Version Numbering, , Programmatic Help Strings, Advanced Usage
2321 @section Version Numbering
2324 As @clon{} evolves over time, you might one day feel the need for
2325 conditionalizing your code on the version of the library. While the
2326 end-user of your application has limited means to access the current
2327 version number of @clon{} (see @enduserref{Clonification} and the
2328 built-in option @code{--clon-version}), you, the application programmer
2329 and @clon{} user, have a finer grained access to it.
2331 The first thing you can do to access the current version number of
2332 @clon{} is use the @code{version} function (this is in fact the function
2333 bound to the @code{--clon-version} option).
2335 @defun version &optional (TYPE :number)
2336 Return the current version number of @clon{}. @var{TYPE} can be one of
2337 @code{:number}, @code{:short} or @code{:long}. For @code{:number}, the
2338 returned value is a fixnum. Otherwise, it is a string.
2341 A @clon{} version is characterized by 4 elements as described below.
2344 @vindex +release-major-level+
2345 A major version number stored in the constant
2346 @code{+release-major-level+}.
2348 @vindex +release-minor-level+
2349 A minor version number, stored in the constant
2350 @code{+release-minor-level+}.
2352 @vindex +release-status+
2353 A release status stored in the constant @code{+release-status+}. The
2354 status of a release can be @code{:alpha}, @code{:beta}, @code{:pre} or
2355 @code{:patchlevel}. These are in effect 4 levels of expected stability,
2356 @code{:pre} meaning pre-release and @code{:patchlevel} corresponding to
2357 a stable version of the library.
2359 @vindex +release-status-level+
2360 A status-specific version number stored in the constant
2361 @code{+release-status-level+}. Status levels start at 1 for unstable
2362 versions (@eg{} beta 1, beta 2 @etc{}), but start at 0 for stable
2363 releases (@eg{} 2.4.0, 2.4.1 @etc{}).
2366 @vindex +release-name+
2367 In addition to that, each version of @clon{} (in the sense
2368 @emph{major.minor}, regardless of the status) has a name, stored in the
2369 constant @code{+release-name+}. The general theme for @clon{} is ``Great
2370 Jazz Musicians'', and specifically for the 1.x series: ``Great Saxophone
2371 Players''. Anyone daring to mention Kenny G at that point will be shot
2374 Here is how the @code{version} function computes its value.
2377 A version @code{:number} is computed as @emph{major . 10000 + minor .
2378 100 + patchlevel}, effectively leaving two digits for each level. Note
2379 that alpha, beta and pre status are ignored in version numbers (this is
2380 as if the corresponding status level was considered to be always 0).
2381 Only stable releases have their level taken into account.
2383 A @code{:short} version will appear like this for unstable releases:
2384 1.3a4, 2.5b8 or 4.2pre1. Remember that alpha, beta or pre levels start
2385 at 1. Patchlevels for stable releases start at 0 but 0 is ignored in the
2386 output. So for instance, version 4.3.2 will appear as-is, while version
2387 1.3.0 will appear as just 1.3.
2389 A @code{:long} version is expanded from the short one, and includes the
2390 release name. For instance, 1.3 alpha 4 "Bill Evans", 2.5 beta 8 "Scott
2391 Henderson", 4.2 pre 1 "Herbie Hancock" or 4.3.2 "Chick Corea". As for
2392 the short version, a patchlevel of 0 is ignored in the output: 1.3 "Bill
2398 @c ====================================================================
2400 @c ====================================================================
2401 @node Conclusion, Quick Start Program, Advanced Usage, Top
2404 So that's it I guess. You know all about @clon{} now. The next step is
2405 to actually use it to clonify your favorite application, write new
2406 applications using it and contaminate the world with standalone @cl{}
2407 programs, featuring unprecedented @cmdline{} power and thrill-a-minute
2410 Now, go Luke. The Attack of the @clon{} is ready to begin.
2414 @c ====================================================================
2415 @c Quick Start Program
2416 @c ====================================================================
2417 @node Quick Start Program, API Quick Reference, Conclusion, Top
2418 @appendix Quick Start Program
2422 (in-package :cl-user)
2425 (asdf:operate 'asdf:load-op :com.dvlsoft.clon)
2426 (use-package :com.dvlsoft.clon)
2428 (defsynopsis (:postfix "FILES...")
2429 (text :contents "A very short program.")
2430 (group (:header "Immediate exit options:")
2431 (flag :short-name "h" :long-name "help"
2432 :description "Print this help and exit.")
2433 (flag :short-name "v" :long-name "version"
2434 :description "Print version number and exit.")))
2437 "Entry point for our standalone application."
2439 (when (getopt :short-name "h")
2442 (do-cmdline-options (option name value source)
2443 (print (list option name value source)))
2447 (dump "test" #'main)
2453 @c ====================================================================
2455 @c ====================================================================
2456 @node API Quick Reference, Indexes, Quick Start Program, Top
2457 @appendix API Quick Reference
2460 * Utilities:: Miscellaneous stuff
2461 * Initialization Phase API:: Synopsis, items and context creation
2462 * Runtime Phase API:: Option retrieval and help formatting
2463 * Extension API:: New option types
2464 * Versioning API:: Release identification
2471 @node Utilities, Initialization Phase API, API Quick Reference, API Quick Reference
2474 @defun dump NAME FUNCTION
2475 See @ref{Quick Start}.
2478 @defun exit &optional (STATUS 0)
2479 See @ref{Quick Start}.
2482 @cindex Command-Line
2484 See @ref{Option Retrieval}.
2487 @cindex Package, nicknames
2488 @defun nickname-package &optional NICKNAME
2489 See @ref{Quick Start}.
2493 @c ------------------------
2494 @c Initialization Phase API
2495 @c ------------------------
2496 @node Initialization Phase API, Runtime Phase API, Utilities, API Quick Reference
2497 @section Initialization Phase API
2499 @defun make-text &key CONTENTS HIDDEN
2500 @cfsubindex{make-text}
2501 @cfiaindex{make-text,:contents}
2502 @cfiaindex{make-text,:hidden}
2503 @defunx make-@var{OPTION} :INITARG INITVAL@dots{}
2504 @cfsubindex{make-@var{OPTION}}
2506 @cfindex make-stropt
2507 @cfindex make-lispobj
2510 @cfindex make-switch
2511 @cfindex make-xswitch
2512 @defunx make-group &key HEADER HIDDEN ITEM
2513 @cfsubindex{make-group}
2514 @cfiaindex{make-group,:header}
2515 @cfiaindex{make-group,:hidden}
2516 @cfiaindex{make-group,:item}
2517 @defunx make-synopsis &key POSTFIX ITEM (MAKE-DEFAULT t)
2518 @cfsubindex{make-synopsis}
2519 @cfiaindex{make-synopsis,:postfix}
2520 @cfiaindex{make-synopsis,:item}
2521 @cfiaindex{make-synopsis,:make-default}
2522 See @ref{Constructors} and @ref{Using Different Synopsis}.
2525 @defmac defgroup (&key HEADER HIDDEN) &body FORMS
2526 @foindex{defgroup,:header}
2527 @foindex{defgroup,:hidden}
2528 @xref{Group Definition}.
2531 @defmac defsynopsis (&key POSTFIX MAKE-DEFAULT) &body FORMS
2532 @foindex{defsynopsis,:postfix}
2533 @foindex{defsynopsis,:make-default}
2534 @fiindex{defsynopsis,text}
2535 @fiindex{defsynopsis,options}
2536 @fiindex{defsynopsis,group}
2537 See @ref{Synopsis Definition} and @ref{Using Different Synopsis}.
2540 @defopt *default-synopsis*
2541 @xref{Using Different Synopsis}.
2544 @defun make-context &key (SYNOPSIS *default-synopsis*) CMDLINE @
2546 @cfsubindex{make-context}
2547 @cfiaindex{make-context,:synopsis}
2548 @cfiaindex{make-context,:cmdline}
2549 @cfiaindex{make-context,:make-current}
2550 @xref{Context Creation}, @ref{Using Different Synopsis} and @ref{Using
2551 Different Command-Lines}.
2556 @c -----------------
2557 @c Runtime Phase API
2558 @c -----------------
2559 @node Runtime Phase API, Extension API, Initialization Phase API, API Quick Reference
2560 @section Runtime Phase API
2562 @defopt *current-context*
2563 @xref{Using Multiple Contexts}.
2566 @defun with-context CONTEXT &body BODY
2567 @xref{Using Multiple Contexts}.
2570 @defun progname &key (CONTEXT *current-context*)
2571 @fkindex{progname,:context}
2572 See @ref{Contextual Information} and @ref{Using Multiple Contexts}.
2575 @defun remainder &key (CONTEXT *current-context*)
2576 @fkindex{remainder,:context}
2577 See @ref{Contextual Information} and @ref{Using Multiple Contexts}.
2580 @defun getopt &key (CONTEXT *current-context*) SHORT-NAME LONG-NAME @
2582 @fkindex{getopt,:context}
2583 @fkindex{getopt,:short-name}
2584 @fkindex{getopt,:long-name}
2585 @fkindex{getopt,:option}
2586 @vindex *current-context*
2587 See @ref{Explicit Retrieval} and @ref{Using Multiple Contexts}.
2590 @defun getopt-cmdline &key (CONTEXT *current-context*)
2591 @fkindex{getopt-cmdline,:context}
2592 @vindex *current-context*
2593 See @ref{Sequential Retrieval} and @ref{Using Multiple Contexts}.
2596 @defmac multiple-value-getopt-cmdline (OPTION NAME VALUE SOURCE &key CONTEXT) @
2598 @defmacx do-cmdline-options (OPTION NAME VALUE SOURCE &key CONTEXT) @
2600 @foindex{multiple-value-getopt-cmdline,:context}
2601 @foindex{do-cmdline-options,:context}
2602 See @ref{Sequential Retrieval} and @ref{Using Multiple Contexts}.
2605 @deffn Reader short-name OPTION
2606 @rfsubindex{short-name}
2607 @deffnx Reader long-name OPTION
2608 @rfsubindex long-name
2609 @xref{Sequential Retrieval}.
2612 @defun help &key (CONTEXT *current-context*) @
2613 (ITEM (synopsis context)) @
2614 (OUTPUT-STREAM *standard-output*) @
2615 (SEARCH-PATH (search-path context)) @
2616 (THEME (theme context)) @
2617 (LINE-WIDTH (line-width context)) @
2618 (HIGHLIGHT (highlight context)))
2619 @fkindex{help,:context}
2620 @fkindex{help,:item}
2621 @fkindex{help,:output-stream}
2622 @fkindex{help,:theme}
2623 @fkindex{help,:search-path}
2624 @fkindex{help,:line-width}
2625 @fkindex{help,:highlight}
2626 @vindex *current-context*
2627 @vindex *standard-output*
2628 @xref{Help}, @ref{Using Multiple Contexts} and @ref{Programmatic Help
2636 @node Extension API, Versioning API, Runtime Phase API, API Quick Reference
2637 @section Extension API
2639 @defmac defoption CLASS SUPERCLASSES SLOTS &rest OPTIONS
2640 @pxref{New Option Classes}
2643 @deffn {Generic Function} check OPTION VALUE
2645 @xref{Value Check Protocol}.
2648 @deftp {Error Condition} option-error OPTION
2649 @ecsubindex{option-error}
2650 @ecsindex{option-error,option}
2651 @deftpx {Error Condition} invalid-value VALUE COMMENT
2652 @ecsubindex{invalid-value}
2653 @ecsindex{invalid-value,value}
2654 @ecsindex{invalid-value,comment}
2655 @xref{Value Check Protocol}.
2658 @deffn {Generic Function} convert OPTION ARGUMENT
2659 @gfsubindex{convert}
2660 @xref{Argument Conversion Protocol}.
2663 @deftp {Error Condition} invalid-argument ARGUMENT COMMENT
2664 @ecsubindex{invalid-argument}
2665 @ecsindex{invalid-argument,argument}
2666 @ecsindex{invalid-argument,comment}
2667 @xref{Argument Conversion Protocol}.
2670 @deffn {Generic Function} stringify OPTION VALUE
2671 @gfsubindex{stringify}
2672 @xref{Value Stringification Protocol}.
2679 @node Versioning API, , Extension API, API Quick Reference
2680 @section Versioning API
2682 @defun version &optional (TYPE :number)
2683 @xref{Version Numbering}.
2686 @defvr Constant +release-major-level+
2687 @defvrx Constant +release-minor-level+
2688 @defvrx Constant +release-status+
2689 @defvrx Constant +release-status-level+
2690 @defvrx Constant +release-name+
2691 @xref{Version Numbering}.
2696 @c ====================================================================
2698 @c ====================================================================
2699 @node Indexes, , API Quick Reference, Top
2703 * Concept Index:: Well, the concept index
2704 * Function Index:: Well, the function index
2705 * Variable Index:: Well, the variable index
2706 * Data Type Index:: Well, the data type index
2713 @node Concept Index, Function Index, Indexes, Indexes
2722 @node Function Index, Variable Index, Concept Index, Indexes
2731 @node Variable Index, Data Type Index, Function Index, Indexes
2740 @node Data Type Index, , Variable Index, Indexes
2747 @c LocalWords: Clon clon cmdline Clonification tty emph CmdLine clonified SGR
2748 @c LocalWords: clonfiscated clonistified clonificated clonificationated samp
2749 @c LocalWords: cindex subsubsection pxref Didier Nuker postfix Runtime cth
2750 @c LocalWords: cartouche toplevel prepended IEC
2752 @c user.texi ends here