Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Fwd: st: -args- how to flag when missing or too many arguments
From
John Antonakis <[email protected]>
To
[email protected]
Subject
Re: Fwd: st: -args- how to flag when missing or too many arguments
Date
Fri, 22 Mar 2013 11:39:48 +0100
Thanks Nick. Very helpful indeed. And, right, it is for an immediate
version of program I am writing.
Best,
J.
__________________________________________
John Antonakis
Professor of Organizational Behavior
Director, Ph.D. Program in Management
Faculty of Business and Economics
University of Lausanne
Internef #618
CH-1015 Lausanne-Dorigny
Switzerland
Tel ++41 (0)21 692-3438
Fax ++41 (0)21 692-3305
http://www.hec.unil.ch/people/jantonakis
Associate Editor
The Leadership Quarterly
__________________________________________
On 22.03.2013 11:21, Nick Cox wrote:
Here is another way to approach it, building on earlier suggestions.
numlist "`1' `2' `3' `4' `5'", int min(4) max(4)
args a b c d
If 3 or fewer arguments are specified, the test will fail.
If 5 or more arguments were specified, "`5'" will be non-empty and the
test will fail. But you must mention `5' for it to test.
Or you could just use `0'.
numlist "`0'", int min(4) max(4)
With this, we need not name the arguments until we know we have the
right number.
If the logic is right, then the differences are mostly of taste,
except that simplicity and clarity should appeal universally.
Details differ if there are other elements to the syntax, such as options.
A program that takes on only numerical arguments might qualify as an
"immediate" command. Start with -help immediate- for suggestions. It's
a good convention that immediate command names end with "i", although
the converse need not be true.
Nick
On Fri, Mar 22, 2013 at 8:47 AM, John Antonakis <[email protected]> wrote:
OK. Thanks Nick.....and I meant to say "doesn't do anything".
Best,
J.
__________________________________________
John Antonakis
Professor of Organizational Behavior
Director, Ph.D. Program in Management
Faculty of Business and Economics
University of Lausanne
Internef #618
CH-1015 Lausanne-Dorigny
Switzerland
Tel ++41 (0)21 692-3438
Fax ++41 (0)21 692-3305
http://www.hec.unil.ch/people/jantonakis
Associate Editor
The Leadership Quarterly
__________________________________________
On 22.03.2013 09:38, Nick Cox wrote:
---------- Forwarded message ----------
From: Nick Cox <[email protected]>
Date: Fri, Mar 22, 2013 at 8:30 AM
Subject: Re: st: -args- how to flag when missing or too many arguments
To: [email protected]
John: You should know that the Statalist FAQ advises against "does not
work" as an error report.
if "`chi'" == ""
is sufficient as a test of there not being four arguments. Whether
there are none, one, two, or three, it follows in each case that there
was no fourth argument.
Your code does not test that the arguments are integers (which you can
do in one line with -numlist-).
Nick
On Fri, Mar 22, 2013 at 7:51 AM, John Antonakis <[email protected]>
wrote:
Thanks Nick. That works.
With respect to flagging less than four Austin gave me some nice code
that
works. Following Nick's approach, the below doesn't work and I can't see
why:
...
args no_vars df N chi too_much
*nick's part, works
if "`too_much'" != "" {
di as err "Too many arguments. "
exit 198
}
*my part, does not work
if "`no_vars'"=="" | "`df'"=="" | "`N'"=="" | "`chi'"=="" {
di as err "You have to use four integers. "
exit 498
}
Best,
J.
__________________________________________
John Antonakis
Professor of Organizational Behavior
Director, Ph.D. Program in Management
Faculty of Business and Economics
University of Lausanne
Internef #618
CH-1015 Lausanne-Dorigny
Switzerland
Tel ++41 (0)21 692-3438
Fax ++41 (0)21 692-3305
http://www.hec.unil.ch/people/jantonakis
Associate Editor
The Leadership Quarterly
__________________________________________
On 22.03.2013 02:46, Nick Cox wrote:
My approach for these problems is -- in a case with only 4 arguments
wanted --
args a b c d garbage
if "`garbage'" != ""
di as err "too many arguments"
exit 198
{
On Thu, Mar 21, 2013 at 10:14 PM, John Antonakis
<[email protected]>
wrote:
Thanks, Austin.
This works too, except for when w > 4; it does not display the error.
The
program just ignores any arguments beyond 4 arguments (as is the
default
in
Stata).
Best,
J.
__________________________________________
John Antonakis
Professor of Organizational Behavior
Director, Ph.D. Program in Management
Faculty of Business and Economics
University of Lausanne
Internef #618
CH-1015 Lausanne-Dorigny
Switzerland
Tel ++41 (0)21 692-3438
Fax ++41 (0)21 692-3305
http://www.hec.unil.ch/people/jantonakis
Associate Editor
The Leadership Quarterly
__________________________________________
On 21.03.2013 22:54, Austin Nichols wrote:
John Antonakis <[email protected]>:
You might also want to step through various conditions and then issue
a more informative error message at the end, e.g. consider adapting
this approach:
cap prog drop john
prog john, rclass
version 12
loc err 0
args no_vars df N chi
loc w: word count `no_vars' `df' `N' `chi'
if `w'>4 {
loc err 1
di in red "More than 4 arguments given"
}
foreach v in no_vars df N chi {
cap confirm integer number `=``v'''
if _rc!=0 {
loc err 1
di in red "Arguments must be integers."
}
else if ``v''<0 {
loc err 1
di in red "Arguments must be positive integers."
}
}
if `err'==1 {
di in red "Please enter data after the command" _c
di in red " john as follows: vars df N chi"
exit 198
}
end
On Thu, Mar 21, 2013 at 5:49 PM, daniel klein
<[email protected]>
wrote:
John,
I think I would address this with something like this
cap pr drop john
pr john
vers 12.1
args a b c d
if !inrange(`: word count `0'', 1, 4) {
di as err "Please enter data after " ///
"the command john as follows: vars df N chi"
e 198
}
numlist "`0'" ,int r(>0)
end
Best
Daniel
--
Hi:
I am writing a program and would like Stata to return an error if an
argument is missing or there are too many of them.
For example, if an argument is not an integer, the below will return
an
error:
program define john, rclass
version 12
args no_vars df N chi
if `no_vars'<0 | `df'<0 | `N'<0 | `chi'<0 {
di in red "You have to use integers."
exit 498
}
end
Now, how do I make Stata display:
di in red "Please enter data after the command john as follows: vars
df
N
chi"
.....in cases where (a) there is a missing value (i.e., the user
missed putting 4 numbers) and (b) if the user provides too many
numbers (i.e., 5 instead of 4).
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/