2011-12-16 46 views
19

मुझे लगता है कि प्रपत्रमैं स्ट्रिंग को कैसे बना सकता हूं?

host[:port] 

जहां पेट और बंदरगाह वैकल्पिक हैं में एक मेजबान पहचानकर्ता के होते हैं मेरी Makefile में एक पैरामीटर लेने के लिए की जरूरत है। तो निम्न में से सभी मान्य हैं:

foo.example.com 
ssl.example.com:443 
localhost:5000 

आदि

मैं, वैकल्पिक पेट पर स्ट्रिंग विभाजित है और चर के प्रदान करना चाहते हैं, जिससे कि HOST शामिल foo.example.com, ssl.example.com, localhost, आदि , और PORT क्रमशः 80 (डिफ़ॉल्ट पोर्ट), 443, और 500 शामिल हैं।

उत्तर

34
# Retrieves a host part of the given string (without port). 
# Param: 
# 1. String to parse in form 'host[:port]'. 
host = $(firstword $(subst :, ,$1)) 

# Returns a port (if any). 
# If there is no port part in the string, returns the second argument 
# (if specified). 
# Param: 
# 1. String to parse in form 'host[:port]'. 
# 2. (optional) Fallback value. 
port = $(or $(word 2,$(subst :, ,$1)),$(value 2)) 

उपयोग:

$(call host,foo.example.com) # foo.example.com 
$(call port,foo.example.com,80) # 80 

$(call host,ssl.example.com:443) # ssl.example.com 
$(call port,ssl.example.com:443,80) # 443 
संबंधित मुद्दे