Score:0

is it possible to dynamically call a function via cli in a python script

in flag

I am trying to call a specific function when invoking this python script, i,e i have multiple functions within this script. I tried using globals but not dough.

#!/usr/bin/python3

"""
This module is used to access the service account key stored in the secret manager secret
"""


import sys
import argparse

import json
import datetime
import argparse

def parse_arguments():

    parser = argparse.ArgumentParser(description='Service account key credential', formatter_class=argparse.RawDescriptionHelpFormatter)

    parser.add_argument('-p', '--project_name',
                        metavar='<project_name>',
                        help='GCP project name to retrieve the service account key credential stored in secret manager secret',
                        required=True)

    parser.add_argument('-n', '--secret_name',
                        metavar='<secret_name>',
                        help='name of the secret where service account credential is stored',
                        required=True)

    parser.add_argument('-e', '--email_id',
                        metavar='<email_id>',
                        help='name of the secret where service account credential is stored',
                        required=False)

    parsers = parser.parse_args()
    return parsers


def sa_credentials():
    """
    This is the main method to retrieve the secret manager secret which consists of the service account credential
    """
    options = parse_arguments()
    print(options)

    project_name = options.project_name
    secret_name  = options.secret_name

    print("It worked")

if __name__ == "__main__":
    globals()[sys.argv[1]]

This is how i am running it, but nothing happens

# ./test.py sa_credentials -p the-webbing-330212 -n my-secret
Score:1
cn flag

It's not a good practise to invoke a method via argument, it can be a security fault

You should use something like python test.py 'call_function_one' [...]

And then in your script, you have to get the argument and do :

if (arg[0] == 'call_function_one'):
  function_one(parameters)

An other thing, you can directly parse your argument in your main, it's a common way to use argument_parser, and then calling your functions :)

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.