Splunk Search

Custom Search Commands: how to process positional args in command class?

aovsiannikov
Explorer

i.e. we have some custom command generatetext

@Configuration()
class GenerateTextCommand(GeneratingCommand):

    count = Option(require=True, validate=validators.Integer(0))
    text = Option(require=True)

    def generate(self):
        text = self.text
        for i in range(1, self.count + 1):
            yield {'_serial': i, '_time': time.time(), '_raw': six.text_type(i) + '. ' + text}

I'd like to pass args to it in positional manner,

NOT
| generatetext count=10 text='hi'

BUT
| generatetext 10 hi

How!?

Tags (2)
0 Karma

HackerHit
Engager

I have the same question. Now I figure it out. I think it will help you.

We misunderstand the definition of Field and Option.

So we miss the important property named fieldnames that defined in splunklib/searchcommands/search_command.py Line:201

Following code is what you want. Line 8-9

@Configuration()
class GenerateTextCommand(GeneratingCommand):
    # options
    .....

    def generate(self):
            # field names
            count = self.fieldnames[0] 
            text = self.fieldnames[1]

        for i in range(1, self.count + 1):
            yield {'_serial': i, '_time': time.time(), '_raw': six.text_type(i) + '. ' + text}

Thank you.

0 Karma

HackerHit
Engager

I have the same question. Any body can answer it ?

0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...