Counting Keyword set in a procedure! [message #81898] |
Fri, 26 October 2012 07:22 |
Vincent Sarago
Messages: 34 Registered: September 2011
|
Member |
|
|
Dear IDLers,
I'm taking the risk of nice comments from David (and others) about the usefulness of such a task... (But it was just for answering a post on the EXELIS Forum: http://www.exelisvis.com/language/fr-FR/UserCommunity/UserFo rums/forumid/27/postid/13592/scope/posts.aspx)
The idea is to count the number of keywords set like n_params() does for parameters inside a procedure.
Here is my version:
;+
; Get the number of keyword set in a procedure.
;
; :Returns: integer
;
; :Examples:
; PRO procedure_name, param0, KEYWORD1=KEYWORD1, KEYWORD2=KEYWORD2, KEYWORD3=KEYWORD3
; PRINT, 'PARAM NUMBER: ', n_params()
; PRINT, 'KEYWORD NUMBER: ', vs_n_keywords()
; END
;
; IDL> procedure_name, fltarr(100), /KEYWORD1
; IDL Console :
; PARAM NUMBER: 1
; KEYWORD NUMBER: 1
;
; :Restrictions:
; Keywords have to be written properly:
; KEYWORD1=KEYWORD1
; not like:
; KEYWORD1=KEYWORD
;
; :Author:
; Vincent SARAGO
; Montreal, QC
; +1 514 779 3031
; vincent.sarago@gmail.com
; http://vincentsarago.wordpress.com
;
; :History:
; 1.0 - 25 Oct. 2012 (V.S) - Initial Work
;
; :Version: 1.0
;
;-
Function VS_N_KEYWORDS
compile_opt idl2, hidden
catch, theerror
if (theerror ne 0) then begin
catch, /cancel
ok = dialog_message(!error_state.msg, /error)
return, -1
endif
ii = 0L
;find routines names and informations
res = n_elements(scope_traceback()) ; number of routine calls that have brought idl execution to the current point
if res le 2 then return, 0 ;if called in $MAIN$
routine_traceback = (scope_traceback(/structure))[res-2] ;routine we want to analyse is in res-2 position
routine_name = routine_traceback.routine
is_function = routine_traceback.is_function ;tag if function
;extract keywords names from routine info
keywords = (routine_info(routine_name, function = is_function, /PARAMETERS)).kw_args
for num = 0L, n_elements(keywords) - 1L do begin
if keyword_set((scope_varfetch(keywords[num], level = -1))) then ii++
endfor
return, ii
End ;{VS_N_KEYWORDS}
----
I see several problems but I think it's a good start.
Cheers,
VIncent
|
|
|