From: Hans Hagen <j.hagen@xs4all.nl>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>,
Christoph Reller <christoph.reller@gmail.com>
Subject: Re: Defining command with optional and mandatory arguments
Date: Thu, 24 May 2018 14:50:36 +0200 [thread overview]
Message-ID: <920789de-9326-1be1-d419-ea481b89a2ac@xs4all.nl> (raw)
In-Reply-To: <CAO8LnPEGj5YZM2DYnQ3bWKWNkT595p4xgrif0--JdrSca-38EA@mail.gmail.com>
On 5/24/2018 11:21 AM, Christoph Reller wrote:
> On Wed, 23 May 2018 16:01:05 +0200, Hans Hagen <j.hagen@xs4all.nl> wrote:
>>
>> On 5/23/2018 3:39 PM, Christoph Reller wrote:
>>> Hi,
>>>
>>> What is the right way to define a command with both mandatory and
>>> optional arguments, e.g:
>>>
>>> \MyCommand[optional][mandatory]
>>>
>>> Consider the following MWE:
>>>
>>> \unexpanded\def\MyCommand[#1]{
>>> \dosingleempty{\doMyCommand[#1]}}
>>> \def\doMyCommand[#1][#2]{
>>> \doifsomething{#1}{number 1: #1\par}
>>> \doifsomething{#2}{number 2: #2}\blank[big]}
>>> \starttext
>>> \MyCommand[A][B]
>>> \MyCommand[A]
>>> \stoptext
>>>
>>> In last year's versions of ConTeXt the output was
>>>
>>> number 1: A
>>> number 2: B
>>> number 1: A
>>>
>>> In the latest version of ConTeXt the output is
>>>
>>> number 1: A
>>> number 2: B
>>> number 2: A
>>>
>>> Is this behavior intended? How can I make a definition whose behavior
>>> does not change in new versions of ConTeXt?
>> i'm not sure wht happens at your end but this is the best way:
>>
>> \unexpanded\def\MyCommand
>> {\dodoubleempty\doMyCommand}
>>
>> \def\doMyCommand[#1][#2]%
>> {\iffirstargument
>> number 1: #1%
>> \par
>> \fi
>> \ifsecondargument
>> number 2: #2%
>> \fi
>> \blank[big]}
>>
>> \starttext
>> \MyCommand[A][B]
>> \MyCommand[A]
>> \stoptext
>
> Thank you Hans for this information. My question is rather about error
> handling. I want:
>
> \MyCommand[A][B] % <- succeeds with #1->A, #2->B
> \MyCommand[A] % <- succeeds with #1->A
> \MyCommand % <- fails with "! Use of \MyCommand doesn't match its definition"
>
> I just wanted to ask whether there is a standard way to achieve this
> with \do<whatever>empty. If not, then this is also okay.
just use \dodoubleempty instead
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : http://contextgarden.net
___________________________________________________________________________________
next prev parent reply other threads:[~2018-05-24 12:50 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-05-24 9:21 Christoph Reller
2018-05-24 12:50 ` Hans Hagen [this message]
-- strict thread matches above, loose matches on Subject: below --
2018-05-25 5:08 Christoph Reller
2018-05-24 9:24 Christoph Reller
2018-05-24 13:17 ` Henning Hraban Ramm
2018-05-24 13:43 ` Taco Hoekwater
2018-05-24 16:07 ` Alan Braslau
2018-05-23 13:39 Christoph Reller
2018-05-23 14:01 ` Hans Hagen
2018-05-23 15:54 ` Henning Hraban Ramm
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=920789de-9326-1be1-d419-ea481b89a2ac@xs4all.nl \
--to=j.hagen@xs4all.nl \
--cc=christoph.reller@gmail.com \
--cc=ntg-context@ntg.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).