从命令行

时间:2015-07-21 19:13:18

标签: python

我没有python经验。我试图弄清楚如何从命令行触发这部分代码。我发现的问题是它看起来像" UserAccount()"是一个对象,我不知道如何从命令行触发它。那么a)UserAccount是什么? b)如何从带命令的命令行调用它?

        # Create a random use account
        randomInt = random.randint(1, 4294967295)
        accountId = "random_id+" + str(randomInt) + "@acme.com"
        randomInt = random.randint(1, 4294967295)
        password = "Random_password1_" + str(randomInt)
        primaryEmail = accountId

        userAccount = UserAccount()
        userAccount.accountId = accountId
        userAccount.password = password
        userAccount.primaryEmail = primaryEmail
        userAccount.firstName = "Random"
        userAccount.lastName = "User"
        userAccount.birthdayMonth = 5
        userAccount.birthdayDay = 31

        #userAccount.firstNamePhonetic = ""
        #userAccount.firstNameRomagi = ""
        #userAccount.middleName = ""
        #userAccount.middleNamePhonetic = ""
        #userAccount.middleNameRomagi = ""
        #userAccount.lastName = ""
        #userAccount.lastNamePhonetic = ""
        #userAccount.lastNameRomagi = ""
        #userAccount.companyName = ""
        #userAccount.securityQuestion = ""
        #userAccount.securityAnswer = ""
        #userAccount.locale = ""
        #userAccount.timeZone = ""
        #userAccount.allowUpdateNotification = False
        #userAccount.allowThirdPartyNotification = False

        # Assume for now that we're just creating a user account, not also requesting activate/deactivate
        # permissions by providing a pre-created OAuth client ID.
        oauthClientId = "";

        print("  Test creating an account: " + userAccount.accountId + ", password: " + userAccount.password + ", email: " + userAccount.primaryEmail)
        accessToken = client.createAccount(userAccount, oauthClientId)
        print("    Account created!")
        if accessToken:
            print("      Access token for account: " + accessToken)
        print()

1 个答案:

答案 0 :(得分:1)

答:UserAccount可能是一个类。 UserAccount()正在调用它的构造函数。请注意,构造是通过调用它而不是new运算符来完成的。 B:正如@OMGtechy已经说过的那样,看看argparse。这里有一个可能对你有用的例子:

import argparse
parser = argparse.ArgumentParser(description='UserAccount creator')

parser.add_argument('--id', '-i', required=True, help='Required. User Account Id')
parser.add_argument('--password', '-p', required=True, help="Required. User Account's Password.")
parser.add_argument('--email', '-e', required=True, help='Required. User Account E-Mail')
parser.add_argument('--firstname', '-f', required=True, help="Required. User Account's first name")
parser.add_argument('--lastname', '-l', required=True, help="Required. User Account's last name")
parser.add_argument('--birthdaymonth', '-m', required=True, help="Required. User Account's Birthday month")
parser.add_argument('--birthdayday', '-d', required=True, help="Required. User Account's Birthday day")

args = parser.parse_args()
userAccount = UserAccount()
userAccount.accountId = args.id
userAccount.password = args.password
userAccount.primaryEmail = args.email
userAccount.firstName = args.firstname
userAccount.lastName = args.lastname
userAccount.birthdayMonth = args.birthdaymonth
userAccount.birthdayDay = args.birthdayday

# Now rest, like normal

oauthClientId = "";

print("  Test creating an account: " + userAccount.accountId + ", password: " + userAccount.password + ", email: " + userAccount.primaryEmail)
accessToken = client.createAccount(userAccount, oauthClientId)
print("    Account created!")
if accessToken:
    print("      Access token for account: " + accessToken)
print()