Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Any easy way to detect invoking of shortcuts, macros & aliases? #1369

Answered by kotfu
Sripadvallabh asked this question in Q&A
Discussion options

I wanted to check the number of times a shortcut or alias or macro was invoked as opposed to the command itself. Is there an easy way to do it?

Currently i feel using a hook & checking the data.statement values:

data.statement.command + data.statement.args != data.statement.raw >>>> MEANS AN INDIRECT COMMAND

But there is no straightforward way to detect if its a macro or shortcut or alias that was invoked.

May i know if this is possible. I just want to have some statistics on how the commands are invoked in general.

You must be logged in to vote

I think you are on the right track, but you'll have to write a little bit of code in your hook.

I'd suggest you utilize a precommand hook. The precommand hook will be called by cmd2 after the user input has been parsed (and the aliases expanded), but before the command is executed. In that hook, you'll have to write a bit of code, which will do two things:

  1. check the data passed into the hook and determine if the alias.macro has been applied
  2. do whatever logging you want

For step 1, you may find the following cmd2 instance variables to be useful:

  • self.aliases - contains a dictionary of all aliases and what they expand to
  • self.macros - contains a dictionary of all macros and what they exp...

Replies: 2 comments 2 replies

Comment options

Off-hand, I don't think we built in any easy way to detect this as we intend them to be fully equivalent.

We have always been completely agnostic when it comes to how end users choose to do logging and metrics.

You must be logged in to vote
1 reply
Comment options

Ok thank you. Thanks for the support towards aliases & macros. It helps a lot

Comment options

I think you are on the right track, but you'll have to write a little bit of code in your hook.

I'd suggest you utilize a precommand hook. The precommand hook will be called by cmd2 after the user input has been parsed (and the aliases expanded), but before the command is executed. In that hook, you'll have to write a bit of code, which will do two things:

  1. check the data passed into the hook and determine if the alias.macro has been applied
  2. do whatever logging you want

For step 1, you may find the following cmd2 instance variables to be useful:

  • self.aliases - contains a dictionary of all aliases and what they expand to
  • self.macros - contains a dictionary of all macros and what they expand to

In your hook you can check if data.statement.raw.split(" ")[0] is in self.aliases or self.macros, then log appropriately.

You must be logged in to vote
1 reply
Comment options

Thanks; i was considering to do this

Answer selected by Sripadvallabh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /