|
2187 | 2187 | \pdfbookmark[1]{getopt\_long}{getoptlong} |
2188 | 2188 |
|
2189 | 2189 | \begin{slide} |
2190 | | -\sltitle{Dlouhý tvar pøepínaèù} |
| 2190 | +\sltitle{Processing long options} |
2191 | 2191 | \setlength{\baselineskip}{0.8\baselineskip} |
2192 | 2192 | \begin{itemize} |
2193 | | -\item poprvé se objevilo v GNU knihovnì \texttt{libiberty}: |
| 2193 | +\item first appeared in GNU library \texttt{libiberty}: |
2194 | 2194 | \begin{verbatim} |
2195 | | - --jméno nebo --jméno=hodnota |
| 2195 | + --name or --name=value |
2196 | 2196 | \end{verbatim} |
2197 | | -\item argumenty se permutují tak, aby pøepínaèe byly na zaèátku, napø. |
2198 | | -\verb#ls * -l# je toté¾ jako \verb#ls -l *#, standardní chování lze docílit |
2199 | | -nastavením promìnné \verb#POSIXLY_CORRECT#. |
2200 | | -\item zpracovávají se funkcí \texttt{\funnm{getopt\_long}()}, která pou¾ívá |
2201 | | -pole struktur popisujících jednotlivé pøepínaèe: |
| 2197 | +\item the arguments are permuted so that e.g. |
| 2198 | +\verb#ls * -l# is the same as \verb#ls -l *#, standard behavior can be enabled |
| 2199 | +by setting the \verb#POSIXLY_CORRECT# environment variable. |
| 2200 | +\item the long options are processed using the \texttt{\funnm{getopt\_long}()} |
| 2201 | +function using these variables and structures: |
2202 | 2202 | {\tt |
2203 | 2203 | \begin{tabbing} |
2204 | 2204 | stru\=ct \funnm{option} \{\\ |
2205 | | -\>const char *name; \textrm{/* jméno pøepínaèe */} \\ |
2206 | | -\>int has\_arg; \textrm{/* hodnota: ano, ne, \emsl{volitelnì} */} \\ |
2207 | | -\>int *flag; \textrm{/*~}\=\textrm{kdy¾ je \texttt{NULL}, funkce vrací |
2208 | | -\texttt{val}, jinak vrací 0}\\\>\>\textrm{a dá \texttt{val} do \texttt{*flag} |
2209 | | -*/} \\ |
2210 | | -\>int val; \textrm{/* návratová hodnota */} \\ |
| 2205 | +\>const char *name; \textrm{/* name of the option */} \\ |
| 2206 | +\>int has\_arg; \textrm{/* value: yes, no, \emsl{optional} */} \\ |
| 2207 | +\>int *flag; \textrm{/*~}\=\textrm{if \texttt{NULL}, the function returns |
| 2208 | +\texttt{val}}, \\ |
| 2209 | +\>\>\textrm{otherwise returns 0} |
| 2210 | +\textrm{and }\texttt{*flag = val}\textrm{ */} \\ |
| 2211 | +\>int val; \textrm{/* return value */} \\ |
2211 | 2212 | \}; |
2212 | 2213 | \end{tabbing}} |
2213 | 2214 | \end{itemize} |
2214 | 2215 | \end{slide} |
2215 | 2216 |
|
2216 | | -verze jak se objevila ve FreeBSD (funkce \textrm{getopt\_long} není |
2217 | | -standarizovaná), má následující vlastnosti: |
2218 | | - |
2219 | | -\begin{itemize} |
2220 | | -\item pokud v¹echny dlouhé pøepínaèe mají nastaveny krátkou variantu |
2221 | | -ve \texttt{val}, je chování \texttt{getopt\_long} kompatibilní s |
2222 | | -\texttt{getopt} |
2223 | | -\item je mo¾né zadávat argument k dlouhému pøepínaèi i s mezerou |
2224 | | -(na\-pøí\-klad \texttt{--color~green}) |
2225 | | -\item pokud je nastaven \texttt{flag}, tak \texttt{getopt\_long} |
2226 | | -vrací 0, èím¾ se tyto dlouhé pøe\-pí\-na\-èe bez krátké varianty zpracují v |
2227 | | -jedné vìtvi pøíkazu \texttt{case} |
2228 | | -\item existuje i volání \texttt{getopt\_long\_only}, které povoluje |
2229 | | -i dlouhé pøepínaèe uvozené jednou uvozovkou (\texttt{-option}) |
2230 | | -\item funkci \texttt{getopt\_long} je mo¾né pou¾ívat dvìmi zpùsoby. |
2231 | | -První zpùsob je, ¾e ka¾dý dlouhý pøepínaè má korespondující krátký |
2232 | | --- takto lze jednodu¹e pøidat dlouhé pøepínaèe do existujícího |
2233 | | -programu a je \emsl{kompatibilní s getopt}. Druhý zpùsob umo¾òuje |
2234 | | -mít samostatné dlouhé pøepínaèe. V tom pøípadì funkce vrací v¾dy 0 |
2235 | | -(nekompatibilita s \texttt{getopt}) a promìnná \texttt{*flag} se |
2236 | | -nastaví na \texttt{val}. |
2237 | | -\item na konkrétním pøíkladu na následující stránce je vidìt, jak to |
2238 | | -celé funguje |
| 2217 | +The version that appeared in FreeBSD (\textrm{getopt\_long} is not part |
| 2218 | +of the standard), has the following properties: |
| 2219 | + |
| 2220 | +\begin{itemize} |
| 2221 | +\item If all long options have the short variant set in \texttt{val}, the |
| 2222 | +behavior of \texttt{getopt\_long} is compatible with that of \texttt{getopt}. |
| 2223 | +\item It is possible to specify value of long option also with a space |
| 2224 | +(for example \texttt{--color~green}). |
| 2225 | +\item If \texttt{flag} is set, \texttt{getopt\_long} returns 0, which makes |
| 2226 | +these long options without short variant processed in single branch of |
| 2227 | +\texttt{case}. |
| 2228 | +\item There is also \texttt{getopt\_long\_only}, that allows long options |
| 2229 | +to begin with single dash (\texttt{-option}). |
| 2230 | +\item The \texttt{getopt\_long} function is possible to use in two ways. |
| 2231 | +The first way is that each long option has a corresponding short variant |
| 2232 | +-- this way it is possible to introduce long options to existing program |
| 2233 | +in \emsl{getopt compatible} way. The second way makes it possible to have |
| 2234 | +independent long options. In this case the function returns always 0 |
| 2235 | +(non-compatible with \texttt{getopt}) and the \texttt{*flag} variable |
| 2236 | +is set to \texttt{val}. |
| 2237 | +\item The next slide demonstrates how it works. |
2239 | 2238 | \end{itemize} |
2240 | 2239 |
|
2241 | 2240 | %%%%% |
2242 | 2241 |
|
2243 | 2242 | \begin{slide} |
2244 | | -\sltitle{Dlouhé pøepínaèe (pokraèování)} |
| 2243 | +\sltitle{Processing long options (cont.)} |
2245 | 2244 | {\tt\begin{tabbing} |
2246 | 2245 | int \funnm{getopt\_long}(\=int \emph{argc}, char * const \emph{argv}[],\\ |
2247 | 2246 | \>const char *\emph{optstring},\\ |
2248 | 2247 | \>const struct option *\emph{longopts},\\ |
2249 | 2248 | \>int *\emph{longindex}); |
2250 | 2249 | \end{tabbing}} |
2251 | 2250 | \begin{itemize} |
2252 | | -\item \texttt{optstring} obsahuje jednopísmenné pøepínaèe, |
2253 | | -\texttt{longopts} obsahuje adresu pole struktur pro dlouhé pøepínaèe |
2254 | | -(poslední záznam pole obsahuje samé nuly) |
2255 | | -\item pokud funkce narazí na dlouhý pøepínaè, vrací odpovídající |
2256 | | -\texttt{val} nebo nulu (pokud \texttt{flag} nebyl \texttt{NULL}), |
2257 | | -jinak je chování shodné s \texttt{getopt}. |
2258 | | -\item do \texttt{*longindex} (kdy¾ není \texttt{NULL}) dá navíc |
2259 | | -in{}dex nalezeného pøepínaèe v \texttt{longopts}. |
| 2251 | +\item \texttt{optstring} contains short options, |
| 2252 | +\texttt{longopts} contains address of array of structures for long options |
| 2253 | +(last record contains all zeroes) |
| 2254 | +\item if the function hits a long option, it returns corresponding |
| 2255 | +\texttt{val} or zero (if \texttt{flag} was not \texttt{NULL}), |
| 2256 | +otherwise the behavior is the same as \texttt{getopt}. |
| 2257 | +\item it will also put the index of the found option in \texttt{longopts} |
| 2258 | +to \texttt{*longindex} (if not \texttt{NULL}) |
2260 | 2259 | \end{itemize} |
2261 | 2260 | \end{slide} |
2262 | 2261 |
|
2263 | 2262 | \begin{itemize} |
2264 | | -\item toto je upravený pøíklad z manuálové stránky na FreeBSD: |
| 2263 | +\item This is slightly modified example from the \texttt{getopt\_long} |
| 2264 | +FreeBSD man page: |
2265 | 2265 | {\footnotesize |
2266 | 2266 | \begin{verbatim} |
2267 | 2267 | #include <stdio.h> |
|
0 commit comments