What Does cfargument Really Do?

It seems obvious that cfargument defines the arguments for your udfs/methods. It is easy, however, to have assumptions about what it does that aren't correct.

Primarily, cfargument defines that arguments that you expect for your function and the order in which you expect them. This helps document your code.

It also acts like cfparam in that it defines a data-type for an argument (throwing an error if the argument isn't of that type) and either making the argument required or providing a default (technically you can do both or neither in cfargument, but that doesn't really make sense. 

It also assigns names to arguments that are passed in ordinally (that is, arguments passed in by order but without a specified name). This allows you to pass in arguments either by name or by order.

What cfargument doesn't do, however, is just as important. It doesn't restrict the arguments that come into your function to those specified by cfargument tags.

So, any arguments that are passed in by name will still exist in the arguments structure of the function. This has both drawbacks and advantages. The drawbacks primarily have to do with security. If you use argumentCollection to pass in a Form structure then any form field will be sent in as an argument.

Among the advantages are the ability to pass in argument names that are not known before hand. It also means that you can have named arguments without any cfargument tags at all (though you lose out on important documentation if you do).

Ideally, I would like to be able to check what arguments have been defined by cfargument from within the tag (so that I could programmatically delete other arguments if I wanted without changing that code every time I added an argument). I suspect it can be done, I just don't know how.

Anyone? 

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Hmm, you want to be able to check what arguments have been defined and which were passed in but not defined? I hate to say it- but don't use cfargument. If your logic is:

I want to know if someone passed argument.foo

then you should simply use structKeyExists on the argument. Shoot, you can do this WITH using the cfargument tag, just don't use a default
# Posted By Raymond Camden | 3/29/07 2:11 PM
Ray,

Sorry, I didn't explain myself well. What I want is just a list (or structure or whatever) of the arguments that have been defined with cfargument, regardless of whether or not they are actually passed in to the method.

The idea being, that I could then use StructDelete() on any incoming named arguments that weren't documented in cfargument.

I don't know if I actually have a real-world need for this, but it seems like it could be handy.

I know this can be done from outside the method. I just don't know how to do it from within the method.
# Posted By Steve Bryant | 3/29/07 7:32 PM
You can check them with the metadata provided through getMetaData().

Specifically, to get the data you're looking for:






# Posted By Dustin | 3/30/07 3:46 PM
I'm back. I figured out how you can shorten the above up a little and do it within the function.


   
         
   
# Posted By Dustin | 3/30/07 6:25 PM
Dustin,

That certainly does what I said I want - and from within the function. I don't like that it requires the code inside of a function to know the name of the function - but I doubt that any better solution is possible.

That is certainly better than anything that I have come up with.

Thanks!
# Posted By Steve Bryant | 3/30/07 9:17 PM
Another thought: it would be great if you could set the CFARGUMENT tag to use your default value when an invalid value is passed instead of having it throw an error. (I think I remember seeing that one someone's blog, but for the life of me I can't find the reference to it.)
# Posted By Tom Mollerus | 4/6/07 2:30 PM
That would be nice!

I don't remember anyone mentioning that about cfargument, but Ben Nadel mentioned that about cfparam.
http://bennadel.com/index.cfm?dax=blog:594.view

I have noticed that I use type="string" a lot where I want type="numeric" and then I have logic in the method to delete the argument or set it to a default numeric value if it isn't a valid number. Always seems like there should be an easier way.
# Posted By Steve Bryant | 4/6/07 2:35 PM
I think it is good.I like it for reference.
# Posted By Efrat | 8/24/07 1:16 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.