वर्तमान में मेरा कोड इस तरह दिखता है। यह मुझे मेरे प्रोग्राम स्क्रिप्ट के कई मानकों को पार्स करने की अनुमति देता है। क्या कोई अलग तरीका है जो 'सर्वोत्तम प्रथाओं' के करीब है? मैंने वास्तव में argparse
के आउटपुट का उपयोग करके कोड नहीं देखा है, केवल इसे कैसे सेट अप करें।कार्यों को कॉल करने के लिए argparse आउटपुट का उपयोग
def useArguments():
x = 0
while x <= 5:
if x == 0:
if args.getweather != None:
getWeather(args.getweather)
if x == 1:
if args.post != None:
post(args.post)
if x == 2:
if args.custompost != None:
custompost(args.custompost)
if x == 3:
if args.list != None:
listAccounts(args.list)
if x == 4:
if args.add != None:
addAccount(args.add[0])
if x == 5:
if args.edit != None:
editAccount(args.edit[0])
x = x + 1
if __name__ == '__main__':
updateConfig()
parser = argparse.ArgumentParser(description='Post Yahoo weather to Twitter.', epilog="Report any bugs to [email protected]", prog='Program')
parser.add_argument('-a', '--add', nargs=1, help='Add a new account. Use the desired account name as an argument.')
parser.add_argument('-e', '--edit', nargs=1, choices=accountListSTR[:-1], help='Edit an account. Use the desired account name as an argument.')
parser.add_argument('-g', '--getweather', nargs='*', choices=accountListSTR, help='Get weather and post here. Specify account(s) as argument. Use "all" for all accounts. If you specify multiple accounts, separate by a space NOT a comma.')
parser.add_argument('-p', '--post', nargs='*', choices=accountListSTR, help='Post weather to Twitter. Specify account(s) as argument. Use "all" for all accounts. If you specify multiple accounts, separate by a space NOT a comma.')
parser.add_argument('-c', '--custompost', nargs=2, help='Post a custom message. Specify an account then type the message. Make sure you use "" around the message. Use "all" for all accounts.')
parser.add_argument('-l', '--list', action='store_const', const='all', help='List all accounts.')
parser.add_argument('--version', action='version', version='%(prog)s 0.3.3')
args = parser.parse_args()
useArguments()
यह किस स्थिति में सबसे अच्छा तरीका होगा? मैं उस अतिरिक्त कोड के लिए उपयोग नहीं देख सकता। लेकिन फिर, मैंने मुश्किल से कक्षाओं का उपयोग किया ताकि मैं शायद कुछ खो रहा हूं। – avacariu
@vlad, इसका उपयोग किसी फ़ंक्शन को स्वचालित रूप से एक फ़ंक्शन पर कॉल करने के लिए किया जा सकता है, जो आप कर रहे हैं वह सब आपके बॉयलरप्लेट - आपको केवल उचित उप-वर्गों के '__call__' विधियों को बनाना होगा 'argparse.Action' का। लेकिन यदि आप ऑब्जेक्ट उन्मुख प्रोग्रामिंग "प्राप्त नहीं करते हैं, तो यह ठीक है, आप इसे अपना रास्ता कर सकते हैं (हालांकि उस लूप और' यदि x ==' चेक किसी भी मामले में वास्तव में अनावश्यक हैं - बस एक के बाद चेक करें उचित कॉल द्वारा संभवतः अनुयायी कौन से तर्क मौजूद हैं, आपके द्वारा उपयोग किए जाने वाले अन्य बॉयलरप्लेट में कोई अतिरिक्त मूल्य नहीं है)। –
इस उत्तर को स्वीकार किया क्योंकि यह मेरे प्रश्न का उत्तर देता है। मैं * यह सीखने के लिए कोशिश कर सकता हूं कि यह कैसे काम करता है; लेकिन इसमें वर्तमान में मेरा कोड कैसे काम करता है (विशेष रूप से वहां सूचीबद्ध फ़ंक्शंस) में कई बदलावों की आवश्यकता होगी। धन्यवाद! – avacariu